From: Daniel DeLorme Date: 2007-06-16T12:19:27+09:00 Subject: Re: Using extend for initialization settings? Trans wrote: > M = { :a => 1 }.to_module(true) > p M.a #=> 1 > > > class Foo > def initialize( settings ) > extend settings.to_module > end > end > > f = Foo.new(:x => 9) > p f.x #=> 9 > > > Thoughts? The behavior is substantially different. With hash initialization, reader and writer accessors are defined in the class. With this technique it seems there's only a reader accessor, and it is only defined per object. What methods the object responds to depend on the initialization values. In effect this seems to create per-object constants rather than traditional "attributes". Maybe I can express myself better with code: class Foo attr_accessor :a, :b end h = Foo.new(:a=>1) h.a #=> 1 h.a=2 #=> 2 h.b #=> nil h.c #=> NoMethodError m = Foo.new(:a=>1) #using to_module m.a #=> 1 m.a=2 #=> NoMethodError m.b #=> NoMethodError m.c #=> NoMethodError s = OpenStruct.new(:a=>1) s.a #=> 1 s.a=2 #=> 2 s.b #=> nil s.c #=> nil If you can find a use for such behavior then it's fine, but I'm afraid I can't. Daniel