From: dblack@... Date: 2007-07-24T19:10:22+09:00 Subject: Re: case equality question Hi -- On Tue, 24 Jul 2007, Phil Meier wrote: > Hello > > I do not understand the following code: > > ====== > file = File.open('test.txt', 'r') > hash = {'file' => file, 'title' => 'title'} > hash.map { | param_key, param_value | > puts "param_key = #{param_key}" > puts "param_value = #{param_value}" > puts "(param_value === String) = #{(param_value === String)}" > case param_value > when String > puts "This is a String!" > when File > puts "This is a File!" > else > puts "Neither String nor File!!!" > end > } > file.close > === > > When I run this code the output is as follows: > param_key = title > param_value = title > (param_value === String) = false > This is a String! > param_key = file > param_value = # > (param_value === String) = false > This is a File! > > Why does Ruby goes to the when String case even if (param_value === String) > is false? Because String === param_value is true :-) That's how the case thing works: obj = Object.new def obj.===(other) puts "I'm being cased!" true end case "blah" when obj end Output: I'm being cased! because obj === "blah" is being called. David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)