[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

what on earth..

Joe Van Dyk

3/23/2006 2:51:00 AM

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


16 Answers

burke

3/23/2006 2:58:00 AM

0

Joe Van Dyk wrote:
> 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
>
>

I'll admit I only started learning Ruby 3 days ago, so don't hold it
against me if I misguide you ;), but I believe switch uses '===' to
compare, which I think is a more precise match...

5.class == Fixnum => true
5.class === Fuxnum => false

You might want to just use ifs instead.

Burke

Timothy Bennett

3/23/2006 2:59:00 AM

0

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 Mar 22, 2006, at 6:51 PM, Joe Van Dyk wrote:

> 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
>



Mike Stok

3/23/2006 3:00:00 AM

0


On 22-Mar-06, at 9:51 PM, Joe Van Dyk wrote:

> 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?

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

ratdog:~/tmp mike$ ri Module#===
------------------------------------------------------------- Module#===
mod === obj => true or false
------------------------------------------------------------------------
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 Stok <mike@stok.ca>
http://www.stok...

The "`Stok' disclaimers" apply.






burke

3/23/2006 3:01:00 AM

0

burke wrote:

> 5.class === Fuxnum => false

Umm, ok. Obviously it's not a Fuxnum. I think I should clear that up:

5.class === Fixnum => false
as well. haha... Sorry about that.

Burke

Timothy Bennett

3/23/2006 3:08:00 AM

0

> case doesn't use == you can do this:
>
> a = 5
>
> case a
> when Fixnum
> puts "I'm a fixnum"
> else
> puts "other"
> end
>
>
> ratdog:~/tmp mike$ ri Module#===
> -------------------------------------------------------------
> Module#===
> mod === obj => true or false
> ----------------------------------------------------------------------
> --
> 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.
>

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


Joe Van Dyk

3/23/2006 3:27:00 AM

0

On 3/22/06, Mike Stok <mike@stok.ca> wrote:
>
> On 22-Mar-06, at 9:51 PM, Joe Van Dyk wrote:
>
> > 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?
>
> case doesn't use == you can do this:
>
> a = 5
>
> case a
> when Fixnum
> puts "I'm a fixnum"
> else
> puts "other"
> end

Very nice, thanks! Less code is always better.


dblack

3/23/2006 3:29:00 AM

0

Timothy Bennett

3/23/2006 3:44:00 AM

0

Ok, this is off the original track of discussion a bit, but I wanted
to bring this up.

irb(main):007:0> 5 === Fixnum
=> false
irb(main):008:0> Fixnum === 5
=> true

irb(main):001:0> 5.class === Class
=> false
irb(main):002:0> Class === 5.class
=> true

I understand why this is happening (I think). I just think this
needs to be fixed. Or is there a reason why === should not be
commutative?

Tim


Mike Stok

3/23/2006 4:08:00 AM

0


On 22-Mar-06, at 10:44 PM, Timothy Bennett wrote:

> Ok, this is off the original track of discussion a bit, but I
> wanted to bring this up.
>
> irb(main):007:0> 5 === Fixnum
> => false
> irb(main):008:0> Fixnum === 5
> => true
>
> irb(main):001:0> 5.class === Class
> => false
> irb(main):002:0> Class === 5.class
> => true
>
> I understand why this is happening (I think). I just think this
> needs to be fixed. Or is there a reason why === should not be
> commutative?

I think of it more as a relationship which has "direction", so in my
view there's nothing to be fixed. In the class comparison it's
testing whether the class of the operand is the same as _or an
ancestor of_ the receiver. So it's not plain same as. (wording taken
from the Pickaxe book)

Mike

--

Mike Stok <mike@stok.ca>
http://www.stok...

The "`Stok' disclaimers" apply.






Caleb Clausen

3/23/2006 4:22:00 AM

0

On 3/22/06, Timothy Bennett <timothy.s.bennett@gmail.com> wrote:
> Ok, this is off the original track of discussion a bit, but I wanted
> to bring this up.
>
> irb(main):007:0> 5 === Fixnum
> => false
> irb(main):008:0> Fixnum === 5
> => true
>
> irb(main):001:0> 5.class === Class
> => false
> irb(main):002:0> Class === 5.class
> => true
>
> I understand why this is happening (I think). I just think this
> needs to be fixed. Or is there a reason why === should not be
> commutative?

For the cases you give, it seems pretty straightforward, but what about this:

Fixnum===Class #false
Class===Fixnum #true

Should Class be considered a Fixnum just because Fixnum is a Class?