snipt

Ctrl+h for KB shortcuts

Ruby

Hash to Class (javascript-like!)

# Converts a hash to an instance of a class
# From http://pullmonkey.com/2008/1/6/convert-a-ruby-hash-into-a-class-object
class Hashit
  def initialize(hash)
    hash.each do |k,v|
      self.instance_variable_set("@#{k}", v)  ## create and initialize an instance variable for this key/value pair
      self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")})  ## create the getter that returns the instance variable
      self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)})  ## create the setter that sets the instance variable
    end
  end
end
https://snipt.net/embed/8c7aba3f7bf35c7ebd292feca286463d/
/raw/8c7aba3f7bf35c7ebd292feca286463d/
8c7aba3f7bf35c7ebd292feca286463d
rb
Ruby
13
2019-06-26T18:23:01
True
False
False
/api/public/snipt/7668/
hash-to-class-javascript-like
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10">10</a> <a href="#L-11">11</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1"># Converts a hash to an instance of a class</span> </span><span id="L-2"><a name="L-2"></a><span class="c1"># From http://pullmonkey.com/2008/1/6/convert-a-ruby-hash-into-a-class-object</span> </span><span id="L-3"><a name="L-3"></a><span class="k">class</span> <span class="nc">Hashit</span> </span><span id="L-4"><a name="L-4"></a> <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="nb">hash</span><span class="p">)</span> </span><span id="L-5"><a name="L-5"></a> <span class="nb">hash</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">k</span><span class="p">,</span><span class="n">v</span><span class="o">|</span> </span><span id="L-6"><a name="L-6"></a> <span class="nb">self</span><span class="o">.</span><span class="n">instance_variable_set</span><span class="p">(</span><span class="s2">&quot;@</span><span class="si">#{</span><span class="n">k</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="c1">## create and initialize an instance variable for this key/value pair</span> </span><span id="L-7"><a name="L-7"></a> <span class="nb">self</span><span class="o">.</span><span class="n">class</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="ss">:define_method</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="nb">proc</span><span class="p">{</span><span class="nb">self</span><span class="o">.</span><span class="n">instance_variable_get</span><span class="p">(</span><span class="s2">&quot;@</span><span class="si">#{</span><span class="n">k</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)})</span> <span class="c1">## create the getter that returns the instance variable</span> </span><span id="L-8"><a name="L-8"></a> <span class="nb">self</span><span class="o">.</span><span class="n">class</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="ss">:define_method</span><span class="p">,</span> <span class="s2">&quot;</span><span class="si">#{</span><span class="n">k</span><span class="si">}</span><span class="s2">=&quot;</span><span class="p">,</span> <span class="nb">proc</span><span class="p">{</span><span class="o">|</span><span class="n">v</span><span class="o">|</span> <span class="nb">self</span><span class="o">.</span><span class="n">instance_variable_set</span><span class="p">(</span><span class="s2">&quot;@</span><span class="si">#{</span><span class="n">k</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span> <span class="n">v</span><span class="p">)})</span> <span class="c1">## create the setter that sets the instance variable</span> </span><span id="L-9"><a name="L-9"></a> <span class="k">end</span> </span><span id="L-10"><a name="L-10"></a> <span class="k">end</span> </span><span id="L-11"><a name="L-11"></a><span class="k">end</span> </span></pre></div> </td></tr></table>
rails, ruby