From: Brian Candler Date: 2010-10-08T05:17:42+09:00 Subject: Re: named parameters in ruby? xiMera wrote: > A.new > a.set_vars( :a => 1. :b =>2 ) > do_some a I think you mean: a = A.new a.set_vars ( :a => 1, :b => 2) do_some a How is this better than do_some :a => 1, :b => 2 ? Note that if you are using ruby 1.9, then you can also write do_some a: 1, b: 2 which (from the caller's point of view) is getting pretty close to named parameters. At the callee's side, multiple assignment keeps things pretty clean: def do_some(v={}) a, b, c = v[:a], v[:b], v[:c] end Or you can use values_at, which is a bit longer in this case. a, b, c = v.values_at(:a, :b, :c) Note that ruby needs to know at *parse* time that a,b and c are local variables, so it can reserve slots for them in the activation record, and this assignment achieves that. Any solution using eval to create local variables would be very inefficient, if it could be made to work at all. irb(main):010:0> def foo irb(main):011:1> eval("x = 5") irb(main):012:1> puts x irb(main):013:1> end => nil irb(main):014:0> foo NameError: undefined local variable or method `x' for main:Object from (irb):12:in `foo' from (irb):14 from :0 -- Posted via http://www.ruby-forum.com/.