Hash#select, different than documented?

The documentation at:
http://www.ruby-doc.org/core/classes/Hash.html#M000750

Says that Hash#select does this:

Returns a new hash consisting of entries for which the block returns
true.

If no block is given, an enumerator is returned instead.

h = { “a” => 100, “b” => 200, “c” => 300 }
h.select {|k,v| k > “a”} #=> {“b” => 200, “c” => 300}
h.select {|k,v| v < 200} #=> {“a” => 100}


However, that is not the behavior I am seeing in my ruby 1.8.6. Is that
documentation for a later version of ruby, in which this behavior
changed? Or is the documentation just wrong?

I instead get an array of pairs returned. From irb:

irb(main):008:0> h = { “a” => 100, “b” => 200, “c” => 300 }
=> {“a”=>100, “b”=>200, “c”=>300}
irb(main):009:0> h.select {|k,v| k > “a”} #=> {“b” => 200, “c” => 300}
=> [[“b”, 200], [“c”, 300]]
irb(main):010:0> h.select {|k,v| v < 200} #=> {“a” => 100}
=> [[“a”, 100]]

Is there any other search behavior implemented on Hash I could be using
instead, with more convenient behavior for finding hash subsets, and/or
finding hash keys whose values are matched by a certain block?

Good Morning

On Thu, Jan 13, 2011 at 9:55 AM, Jonathan R. [email protected]
wrote:

The documentation at:
class Hash - RDoc Documentation

However, that is not the behavior I am seeing in my ruby 1.8.6.

You need to ensure you are looking at the docs for the correct version
of
ruby

Look here

John

However, that is not the behavior I am seeing in my ruby 1.8.6. Is that
documentation for a later version of ruby, in which this behavior
changed? Or is the documentation just wrong?

I remember a mail on the list by James B. stating that ruby-doc.org
has been updated for 1.9 to be the default. I can confirm the expected
behavior (returning a Hash) on 1.9.2 and 1.8.7.

Is there any other search behavior implemented on Hash I could be using
instead, with more convenient behavior for finding hash subsets, and/or
finding hash keys whose values are matched by a certain block?

Passing the return value of select to Hash.[] method might help your
case.