7stud -- wrote:
>
>> So, out of
>> curiosity, is it your understanding that, in Ruby, false includes the
>> absence of a key from a hash?
>>
>
> I don't think of it in such abstract terms. If I the key doesn't exist,
> the '[]' method returns nil.
>
By the way, using fetch like this:
fetch (akey, nil)
returns the exact some thing as the method '[]' (as long as no default
value was specified for a hash when it was created):
h = { "red" => 2, "blue" => 15}
puts h["green"]
puts h.fetch("green", nil)
--output:--
nil
nil
As a result, the way you used fetch in your code doesn't do anything
different than the '[]' method would do.
Here's where fetch would make a difference:
h = Hash.new(10)
h["red"] = 2
h["blue"] = 15
puts h["green"]
puts h.fetch("green", nil)
--output:--
10
nil
h = Hash.new
h["red"] = 2
h["blue"] = 15
puts h["green"]
puts h.fetch("green", true)
--output:--
nil
true
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---