a = 5
puts Fixnum == a.class # spits out true
spits out “Who knows what I am. :-(”
case a.class
when Fixnum
puts “I’m a fixnum!”
else
puts “Who knows what I am. :-(”
end
What’s going on here?
Joe
a = 5
puts Fixnum == a.class # spits out true
case a.class
when Fixnum
puts “I’m a fixnum!”
else
puts “Who knows what I am. :-(”
end
What’s going on here?
Joe
case uses === for comparison, not ==
In my irb, Fixnum === 5.class => false
As for why === gives false, I have no idea. In the mean time, you
could use
case a.class.to_s
when “Fixnum”
puts …
else
puts …
end
Tim
On 22-Mar-06, at 9:51 PM, Joe Van D. wrote:
What’s going on here?
case doesn’t use == you can do this:
a = 5
case a
when Fixnum
puts “I’m a fixnum”
else
puts “other”
end
and by coincidence (not)
ratdog:~/tmp mike$ irb
irb(main):001:0> a = 5
=> 5
irb(main):002:0> Fixnum === a
=> true
Case Equality---Returns +true+ if _anObject_ is an instance of
_mod_ or one of _mod_'s descendents. Of limited use for modules,
but can be used in +case+ statements to classify objects by class.
Hope this helps,
Mike
–
Mike S. [email protected]
http://www.stok.ca/~mike/
The “`Stok’ disclaimers” apply.
class.
Ok, out of the three of us that responded, Mike is the one who knows
what he’s talking about most. Listen to him.
Tim
On 3/22/06, Mike S. [email protected] wrote:
else
case a
when Fixnum
puts “I’m a fixnum”
else
puts “other”
end
Very nice, thanks! Less code is always better.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs