From: Logan Capaldo Date: 2006-03-15T02:27:18+09:00 Subject: Re: Writing Good #each Methods? --Apple-Mail-4-841261084 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed On Mar 14, 2006, at 4:05 AM, Paul Battley wrote: > I'd also add > * If appropriate, inspect the arity of the block and provide the > requested number of parameters. > > Hash#each's behaviour is a good example: > hash = {:a=>1, :b=>2} > hash.each { |x| p x } > # [:b, 2] > # [:a, 1] > hash.each { |k,v| puts "#{k.inspect} => #{v.inspect}" } > # :b => 2 > # :a => 1 > > Paul. This isn't really a function of Hash#each being smart... irb(main):001:0> def example() irb(main):002:1> yield [:a, 1] irb(main):003:1> end => nil irb(main):004:0> example { |x| p x } [:a, 1] => nil irb(main):005:0> example { |k, v| p k; p v; } :a 1 => nil As you can see yielding an array automatically gets you this kind of behavior. There are downsides of course... --Apple-Mail-4-841261084--