From: "Jesús Gabriel y Galán" Date: 2010-04-16T18:37:25+09:00 Subject: Re: Blocks and local variable creation On Fri, Apr 16, 2010 at 11:21 AM, John Lane wrote: > Robert Dober wrote: > >> I do not reallly understand why you want nil, instead of [], but apart >> of this I would do >> select{|_,v| v.include? item}.map(&:first) >> >> or if you insist >> >> inject( nil ){ |r,(k,v)|  v.include?( item ) ? (r||[]) << k : r } > > Can you please explain the line: >> select{|_,v| v.include? item}.map(&:first) > > I've never seen the _ before where I would expect a variable. It's a regular variable name. A block can receive several parameters: select {|k,v| v.include? item} It's a convention that indicates that you are not interested in using that variable. > I also > don't understand (&:first) on the call to map. Search google for Symbol#to_proc, there are many explanations out there that are better than what I would be able to explain (in few words: Ruby tries to convert and object to a proc when you use it like this, calling the to_proc method). > The reason I was returning nil was so I can the write code like this: > > rights_held = rights_for_item (rights_hash, item) > perform_something_with_rights unless rights_hash.nil? > > perhaps I should change that thought process to this: > > rights_held = rights_for_item (rights_hash, item) > perform_something_with_rights unless rights_hash.empty? > > I had thought use of nil was the right way but I'm learning so I'm happy > to be convinced otherwise... Others have already talked about this, and I agree it's better to return the same type of object from a method, and nil and an array are different types. Don't know how to convince you :-). Jesus.