# Setting instance variables from hash parameters (with defaults)

**URL:** <https://rubytalk.org/t/setting-instance-variables-from-hash-parameters-with-defaults/55951>\
**Category:** ruby-talk\
**Created:** [26 October 2009 15:32 UTC](https://rubytalk.org/t/setting-instance-variables-from-hash-parameters-with-defaults/55951 "2009-10-26T15:32:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leslie\_Viljoen1](https://avatars.discourse-cdn.com/v4/letter/l/f1d935/32.png) [@Leslie\_Viljoen1](https://rubytalk.org/u/Leslie_Viljoen1)\
**Post date:** [26 October 2009 15:32 UTC](https://rubytalk.org/t/setting-instance-variables-from-hash-parameters-with-defaults/55951/1 "2009-10-26T15:32:50Z")

</div>

Hi!

I'm sure I might be reinventing the wheel here.  
I was writing the following ugly code in order to set options (with  
defaults) from a hash:

&nbsp;&nbsp;def initialize(opts = {})  
&nbsp;&nbsp;&nbsp;&nbsp;@select\_max = opts.has\_key?(:select\_max) ? opts[:select\_max] : 10000  
&nbsp;&nbsp;&nbsp;&nbsp;@select\_try = opts.has\_key?(:select\_try) ? opts[:select\_try] : 1000  
&nbsp;&nbsp;&nbsp;&nbsp;@min\_sleep\_sec = opts.has\_key?(:min\_sleep\_sec) ? opts[:min\_sleep\_sec] : 5  
&nbsp;&nbsp;&nbsp;&nbsp;@max\_sleep\_sec = opts.has\_key?(:max\_sleep\_sec) ? opts[:max\_sleep\_sec] : 1800  
&nbsp;&nbsp;&nbsp;&nbsp;@default\_sleep = opts.has\_key?(:default\_sleep) ? opts[:default\_sleep] : 10  
....

So to avoid that I wrote:

module Defaulting  
&nbsp;&nbsp;def set\_params(defs, params)  
&nbsp;&nbsp;&nbsp;&nbsp;defs.each do |name, val|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if params.has\_key?(name)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eval "@#{name} = params[name]"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eval "@#{name} = val"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

So that I could do the much nicer:

&nbsp;&nbsp;include Defaulting  
&nbsp;&nbsp;def initialize(opts = {})  
&nbsp;&nbsp;&nbsp;&nbsp;defs = {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:select\_max =\> 10000,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:select\_try =\> 1000,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:min\_sleep\_sec =\> 5,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:max\_sleep\_sec =\> 1800,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:default\_sleep =\> 10  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;set\_params(defs, opts)

....

Note that this allows me to pass options that are explicitly set to nil.

Now:  
1. Is this functionality already tucked away somewhere else?  
2. How can I get rid of those nasty evals?

---

<div class="post-metadata">

**Author:** ![Leslie\_Viljoen1](https://avatars.discourse-cdn.com/v4/letter/l/f1d935/32.png) [@Leslie\_Viljoen1](https://rubytalk.org/u/Leslie_Viljoen1)\
**Post date:** [26 October 2009 15:42 UTC](https://rubytalk.org/t/setting-instance-variables-from-hash-parameters-with-defaults/55951/2 "2009-10-26T15:42:01Z")

</div>

I see this is a shorter way:

module Defaulting  
&nbsp;&nbsp;def set\_params(defs, params)  
&nbsp;&nbsp;&nbsp;&nbsp;defs.merge(params).each {|name, val| eval "@#{name} = val"}  
&nbsp;&nbsp;end  
end

..still, any way to get rid of that eval?

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [26 October 2009 15:45 UTC](https://rubytalk.org/t/setting-instance-variables-from-hash-parameters-with-defaults/55951/3 "2009-10-26T15:45:22Z")

</div>

Hi --

> Hi!
> 
> I'm sure I might be reinventing the wheel here.  
> I was writing the following ugly code in order to set options (with  
> defaults) from a hash:
> 
> &nbsp;&nbsp;def initialize(opts = {})  
> &nbsp;&nbsp;&nbsp;&nbsp;@select\_max = opts.has\_key?(:select\_max) ? opts[:select\_max] : 10000  
> &nbsp;&nbsp;&nbsp;&nbsp;@select\_try = opts.has\_key?(:select\_try) ? opts[:select\_try] : 1000  
> &nbsp;&nbsp;&nbsp;&nbsp;@min\_sleep\_sec = opts.has\_key?(:min\_sleep\_sec) ? opts[:min\_sleep\_sec] : 5  
> &nbsp;&nbsp;&nbsp;&nbsp;@max\_sleep\_sec = opts.has\_key?(:max\_sleep\_sec) ? opts[:max\_sleep\_sec] : 1800  
> &nbsp;&nbsp;&nbsp;&nbsp;@default\_sleep = opts.has\_key?(:default\_sleep) ? opts[:default\_sleep] : 10  
> ....
> 
> So to avoid that I wrote:
> 
> module Defaulting  
> &nbsp;&nbsp;def set\_params(defs, params)  
> &nbsp;&nbsp;&nbsp;&nbsp;defs.each do |name, val|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if params.has\_key?(name)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eval "@#{name} = params[name]"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eval "@#{name} = val"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end
> 
> So that I could do the much nicer:
> 
> &nbsp;&nbsp;include Defaulting  
> &nbsp;&nbsp;def initialize(opts = {})  
> &nbsp;&nbsp;&nbsp;&nbsp;defs = {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:select\_max =\> 10000,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:select\_try =\> 1000,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:min\_sleep\_sec =\> 5,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:max\_sleep\_sec =\> 1800,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:default\_sleep =\> 10  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;set\_params(defs, opts)
> 
> ....
> 
> Note that this allows me to pass options that are explicitly set to nil.
> 
> Now:  
> 1. Is this functionality already tucked away somewhere else?  
> 2. How can I get rid of those nasty evals?

Starting with #2: you can always do:

&nbsp;&nbsp;&nbsp;instance\_variable\_set("@#{name}", value)

For the initialize thing, I would probably do something like this:

&nbsp;&nbsp;&nbsp;class Whatever  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DEFAULTS = {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:select\_max =\> 10000,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:select\_try =\> 1000,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:min\_sleep\_sec =\> 5,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:max\_sleep\_sec =\> 1800,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:default\_sleep =\> 10  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize(opts)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DEFAULTS.update(opts).each do |name, value|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_set("@#{name}", value)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

Another thing to keep in mind for similar cases is that hashes return  
nil (unless you override the default) for non-existent keys. So,  
unless you have a hash where nil might be a valid value, you can do:

&nbsp;&nbsp;&nbsp;h ||= y

rather than checking for a key.

David

> **···**
>
> On Tue, 27 Oct 2009, Leslie Viljoen wrote:
> 
> --  
> The Ruby training with D. Black, G. Brown, J.McAnally  
> Compleat Jan 22-23, 2010, Tampa, FL  
> Rubyist [http://www.thecompleatrubyist.com](http://www.thecompleatrubyist.com)
> 
> David A. Black/Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [29 October 2009 21:41 UTC](https://rubytalk.org/t/setting-instance-variables-from-hash-parameters-with-defaults/55951/4 "2009-10-29T21:41:33Z")

</div>

Facets has #instance\_assign, however the library is transitioning to  
the more flexible instance\_vars.update().

What I usually do of this kind of thing is use setter methods. That  
way you can control what comes in, how it comes in, and bonus! it's  
well documented. Eg. Along the lines of:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DEFAULTS = {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:select\_max =\> 10000,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:select\_try =\> 1000,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:min\_sleep\_sec =\> 5,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:max\_sleep\_sec =\> 1800,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:default\_sleep =\> 10  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize(opts = {})  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DEFAULTS.merge(opts).each do |k,v|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;send("#{k}=", v)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# document me

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :select\_max

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# or, if you need more control

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def select\_max=(val)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@select\_max = val  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# etc...

T.

> **···**
>
> On Oct 26, 11:32 am, Leslie Viljoen \<leslievilj...@gmail.com\> wrote:
> 
> > 1. Is this functionality already tucked away somewhere else?  
> > 2. How can I get rid of those nasty evals?

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [26 October 2009 15:47 UTC](https://rubytalk.org/t/setting-instance-variables-from-hash-parameters-with-defaults/55951/5 "2009-10-26T15:47:06Z")

</div>

Whoops, correction:

> **···**
>
> On Tue, 27 Oct 2009, David A. Black wrote:
> 
> > &nbsp;&nbsp;&nbsp;def initialize(opts)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DEFAULTS.update(opts).each do |name, value|
> 
> That has to be merge, not update.
> 
> David
> 
> --  
> The Ruby training with D. Black, G. Brown, J.McAnally  
> Compleat Jan 22-23, 2010, Tampa, FL  
> Rubyist [http://www.thecompleatrubyist.com](http://www.thecompleatrubyist.com)
> 
> David A. Black/Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [26 October 2009 15:57 UTC](https://rubytalk.org/t/setting-instance-variables-from-hash-parameters-with-defaults/55951/6 "2009-10-26T15:57:29Z")

</div>

One issue with this approach is that with params you can get arbitrary  
instance variables injected, while with the other approach you were  
restricting to the ones you had in defaults. This might be a problem  
or not...

To get rid of the eval, use instance\_variable\_set.

Jesus.

> **···**
>
> On Mon, Oct 26, 2009 at 4:42 PM, Leslie Viljoen \<leslieviljoen@gmail.com\> wrote:
> 
> > I see this is a shorter way:
> > 
> > module Defaulting  
> > def set\_params(defs, params)  
> > defs.merge(params).each {|name, val| eval "@#{name} = val"}  
> > end  
> > end
> > 
> > ..still, any way to get rid of that eval?

---

<div class="post-metadata">

**Author:** ![David\_Masover](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@David\_Masover](https://rubytalk.org/u/David_Masover)\
**Post date:** [29 October 2009 14:19 UTC](https://rubytalk.org/t/setting-instance-variables-from-hash-parameters-with-defaults/55951/7 "2009-10-29T14:19:21Z")

</div>

You want instance\_variable\_set:

module Defaulting  
&nbsp;&nbsp;def set\_params def, params  
&nbsp;&nbsp;&nbsp;&nbsp;defs.merge(params).each {|name, val| instance\_variable\_set name, val}  
&nbsp;&nbsp;end  
end

I can think of a few ways to make it easier to use, and I'd suggest actually  
calling the name= method, rather than setting the instance variable directly.  
Here's a rough sketch:

module Defaulting  
&nbsp;&nbsp;def init\_with\_vars \*vars, &init\_block  
&nbsp;&nbsp;&nbsp;&nbsp;defaults = vars.last.kind\_of?(Hash) ? vars.pop : {}  
&nbsp;&nbsp;&nbsp;&nbsp;vars = (vars + defaults.keys).uniq  
&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor \*vars  
&nbsp;&nbsp;&nbsp;&nbsp;define\_method :initialize do |\*args, &block|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if args.last.kind\_of? Hash  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;defaults.merge(args).each\_pair {|name, val| self.send("#{name}=", val)  
}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if init\_block  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;init\_block.call \*args, &block  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

I'm fairly sure there's something like this already, some combination of  
something like Struct could work. I do like this usage, though:

class Foo  
&nbsp;&nbsp;include Defaulting  
&nbsp;&nbsp;init\_with\_vars :foo, :bar, :baz =\> 'default baz'  
&nbsp;&nbsp;def bar  
&nbsp;&nbsp;&nbsp;&nbsp;'overriding default bar reader'  
&nbsp;&nbsp;end  
end

Mostly because for so many classes, I don't need an initialize method at all,  
except as a convenience to set up variables I know I'll need.

> **···**
>
> On Monday 26 October 2009 10:42:01 am Leslie Viljoen wrote:
> 
> > I see this is a shorter way:
> > 
> > module Defaulting  
> > &nbsp;&nbsp;def set\_params(defs, params)  
> > &nbsp;&nbsp;&nbsp;&nbsp;defs.merge(params).each {|name, val| eval "@#{name} = val"}  
> > &nbsp;&nbsp;end  
> > end
> > 
> > ..still, any way to get rid of that eval?

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [29 October 2009 17:48 UTC](https://rubytalk.org/t/setting-instance-variables-from-hash-parameters-with-defaults/55951/8 "2009-10-29T17:48:51Z")

</div>

David A. Black wrote:

> For the initialize thing, I would probably do something like this:
> 
> &nbsp;&nbsp;&nbsp;class Whatever  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DEFAULTS = {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:select\_max =\> 10000,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:select\_try =\> 1000,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:min\_sleep\_sec =\> 5,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:max\_sleep\_sec =\> 1800,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:default\_sleep =\> 10  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize(opts)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DEFAULTS.update(opts).each do |name, value|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_set("@#{name}", value)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;end

Note: if you want subclasses to be able override the DEFAULTS, then use  
self.class::DEFAULTS instead of just DEFAULTS. Otherwise DEFAULTS will  
statically resolve to Whatever::DEFAULTS.

You might also want to add DEFAULTS.freeze, to prevent you accidentally  
mucking them up (as 'update' does 🙂

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Leslie\_Viljoen1](https://avatars.discourse-cdn.com/v4/letter/l/f1d935/32.png) [@Leslie\_Viljoen1](https://rubytalk.org/u/Leslie_Viljoen1)\
**Post date:** [26 October 2009 19:06 UTC](https://rubytalk.org/t/setting-instance-variables-from-hash-parameters-with-defaults/55951/9 "2009-10-26T19:06:18Z")

</div>

Thanks for the replies!
