From: Lars Olsson Date: 2011-04-14T17:35:25+09:00 Subject: Re: Copying parameters to singleton class On 14 Apr, 03:09, 7stud -- wrote: > 7stud -- wrote in post #992632: > > > Try something like this: > > >    def using(new_options) > >       Class.new(self) do > >         old_options = superclass.options > >         common_keys = old_options.keys & new_options.keys > > >         common_keys.each do |key| > >           old_options[key] = new_options[key] > >         end > > >         @options = old_options > >         @options.freeze > >       end > >     end > > Whoops.  That doesn't work: > > prog.rb:18:in `[]=': can't modify frozen hash (RuntimeError) > > Hmmm...why can you merge() but not directly assign to individual keys > when a hash is frozen? > > Ok, merge() with some better variable names: > >    def using(new_options) >       Class.new(self) do >         old_options = superclass.options > >         @options = old_options.merge(new_options).select do |key| >            old_options.has_key?(key) >         end.freeze >       end >     end > > -- > Posted viahttp://www.ruby-forum.com/. Hi! Even though I agress that using the same name for both the local parameter and a method with the same name might be a bad idea, my code still works without any problem. If I redefine self using to: (minor change to last version. I split up the calls to merge, select, and freeze to its own rows.) def self.using(options) Class.new(self) do puts "Superclass options: #{superclass.options.inspect}" puts "Options from parameter: #{options}" @options = superclass.options.merge(options) @options.select! { |key, value| superclass.options.has_key? (key) } @options.freeze end end it clearly shows that options (by itself) refers to the local paramter and not the class name. /lasso