Quantcast
Channel: instance_variable_set in constructor - Stack Overflow
Viewing all articles
Browse latest Browse all 5

instance_variable_set in constructor

$
0
0

I've made a constructor like this:

class Foo
  def initialize(p1, p2, opts={})
    #...Initialize p1 and p2
    opts.each do |k, v|
      instance_variable_set("@#{k}", v)
    end
  end
end

I'm wondering if it's a good practice to dynamically set instance variables like this or if I should better set them manually one by one as in most of the libs, and why.


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>