From: "David A. Black" Date: 2008-07-01T21:48:47+09:00 Subject: Re: Hash#select returns an array but Hash#reject returns a hash... Hi -- On Tue, 1 Jul 2008, Srijayanth Sridhar wrote: >> >> that will change in newer version of ruby, eg ruby1.9 >> >> irb(main):015:0> RUBY_VERSION >> => "1.9.0" >> irb(main):016:0> a=Hash.new >> => {} >> irb(main):017:0> a[1]=2 >> => 2 >> irb(main):018:0> a[2]=2 >> => 2 >> irb(main):019:0> a[3]=4 >> => 4 >> irb(main):020:0> a.select { |key,value| value > 2 } >> => {3=>4} >> irb(main):021:0> a.reject { |key,value| value <= 2 } >> => {3=>4} >> >> kind regards -botp >> >> > > Thanks. > > So this is just some sort of artefact/legacy code that never got changed? You can actually make a case that select and reject are not exactly symmetrical operations. Imagine a line of people: Joe John Joan David Jim Jenny Jeff Matz If I tell everyone in the line whose name does not begin with J to step backwards (reject), the original line is smaller but it's still the same line. If I tell everyone whose name *does* begin with J to step forwards (select), I've got a new line of J people. "Same line" and "new line" don't necessarily map to "same object" and "new object" in Ruby (since the post-reject hash is a different hash). But it suggests that there's a difference, arguably, between select and reject, in terms of the formal disturbance of the object, which in turn makes it easier to understand why select would return objects in a different container, while reject would leave the container in the same form but just contain fewer things. However, that doesn't mean it's bad for select to return a hash (which it does, as was mentioned, as of 1.9). Just that the behavior of one doesn't necessarily imply the behavior of the other. David -- Rails training from David A. Black and Ruby Power and Light: Intro to Ruby on Rails July 21-24 Edison, NJ Advancing With Rails August 18-21 Edison, NJ See http://www.rubypal.com for details and updates!