[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Class ===

RubyTalk@gmail.com

8/29/2008 12:35:00 AM

I need help with === and Objects

270 Array === Array
>] false [<
290 Array == Array
>] true [<
300 Object === Object
>] true [<
310 Object.class
>] Class [<
320 Object === Class
>] true [<


I would think that Array === Array. If not I would not expect line 300
to be true.

Thanks for any help,
Becker

18 Answers

David A. Black

8/29/2008 12:47:00 AM

0

Hi --

On Fri, 29 Aug 2008, RubyTalk@gmail.com wrote:

> I need help with === and Objects
>
> 270 Array === Array
>> ] false [<
> 290 Array == Array
>> ] true [<
> 300 Object === Object
>> ] true [<
> 310 Object.class
>> ] Class [<
> 320 Object === Class
>> ] true [<
>
>
> I would think that Array === Array. If not I would not expect line 300
> to be true.

Those are two very different cases, though. Given this:

a === b

it is true if the class of b has a in its ancestry. So you can always
try:

b.class.ancestors.include?(a)

If you plug Object/Object into that expression, you'll get true. If
you plug Array/Array in, you won't. The reason is that all objects are
instances of Object and its descendants, including the class object
Object. So Object has a unique relationship, among classes, to itself.


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL
See http://www.r... for details and updates!

Patrick Doyle

8/29/2008 12:52:00 AM

0

and I would also like to know why

x = Array.new()
=> []
x === Array
=> false

I've just got to teach to use:

case x
when Array
...
end

instead of
if x === Array
...
end

...the few times I want to do something only when x is an array.

--wpd

David A. Black

8/29/2008 12:53:00 AM

0

[I seem to have added the letter 'B' to the subject line when I
replied, so I'm replying again without doing so.]

Hi --

On Fri, 29 Aug 2008, RubyTalk@gmail.com wrote:

> I need help with === and Objects
>
> 270 Array === Array
>> ] false [<
> 290 Array == Array
>> ] true [<
> 300 Object === Object
>> ] true [<
> 310 Object.class
>> ] Class [<
> 320 Object === Class
>> ] true [<
>
>
> I would think that Array === Array. If not I would not expect line 300
> to be true.

Those are two very different cases, though. Given this:

a === b

it is true if the class of b has a in its ancestry. So you can always
try:

b.class.ancestors.include?(a)

If you plug Object/Object into that expression, you'll get true. If
you plug Array/Array in, you won't. The reason is that all objects are
instances of Object and its descendants, including the class object
Object. So Object has a unique relationship, among classes, to itself.


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL
See http://www.r... for details and updates!

David A. Black

8/29/2008 12:59:00 AM

0

Hi --

On Fri, 29 Aug 2008, Patrick Doyle wrote:

> and I would also like to know why
>
> x = Array.new()
> => []
> x === Array
> => false

Because the instance method Array#=== is not defined as a test of
class membership. I believe it's the default ===, which is the same as
==.

> I've just got to teach to use:
>
> case x
> when Array
> ...
> end
>
> instead of
> if x === Array
> ...
> end
>
> ...the few times I want to do something only when x is an array.

You can do:

if Array === x

=== is the case equality operator, so this:

case obj
when x

is like this:

if x === obj


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL
See http://www.r... for details and updates!

Doug Glidden

8/29/2008 1:02:00 AM

0

Patrick Doyle wrote:
> and I would also like to know why
>
> x = Array.new()
> => []
> x === Array
> => false
>
> I've just got to teach to use:
>
> case x
> when Array
> ...
> end
>
> instead of
> if x === Array
> ...
> end
>
> ...the few times I want to do something only when x is an array.
>
> --wpd

Forgive me if I'm missing something obvious, but wouldn't it be a whole
lot more logical to do this?

if x.is_a? Array
...
end

- Doug
--
Posted via http://www.ruby-....

Patrick Doyle

8/29/2008 1:07:00 AM

0

On Thu, Aug 28, 2008 at 8:59 PM, David A. Black <dblack@rubypal.com> wrote:
> Hi --
> On Fri, 29 Aug 2008, Patrick Doyle wrote:
>> and I would also like to know why
>>
>> x = Array.new()
>> => []
>> x === Array
>> => false
>
>> I've just got to teach [myself] to use:
>>
>> case x
>> when Array
>> ...
>> end
>>
>> instead of
>> if x === Array
>> ...
>> end
>>
>> ...the few times I want to do something only when x is an array.
>
> You can do:
>
> if Array === x
>
> === is the case equality operator, so this:
>
> case obj
> when x
>
> is like this:
>
> if x === obj

Ahhh.... that makes perfect sense! (And I mean that in the "light
dawns on marble head" sort of way, not in the sarcastic sort of way.)

Thanks....

BTW... I've not had any luck Googling "ruby === class type" sorts of
stuff. So I just figured I would have to live w/o knowing the answer
to this puzzling question.

And now I know the answer.

And the answer makes intuitive sense.

Life is good.

Thanks again :-)

--wpd

Patrick Doyle

8/29/2008 1:08:00 AM

0

> Forgive me if I'm missing something obvious, but wouldn't it be a whole
> lot more logical to do this?
>
> if x.is_a? Array
> ...
> end
yup... that makes good sense too.

This is a great night! :-)

--wpd

David A. Black

8/29/2008 1:20:00 AM

0

Hi --

On Fri, 29 Aug 2008, Patrick Doyle wrote:

> BTW... I've not had any luck Googling "ruby === class type" sorts of
> stuff. So I just figured I would have to live w/o knowing the answer
> to this puzzling question.

If you ever want or need to, you can google for ruby "case equality"
and you'll find links.

> And now I know the answer.
>
> And the answer makes intuitive sense.
>
> Life is good.
>
> Thanks again :-)

Glad to help!


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL
See http://www.r... for details and updates!

Doug Glidden

8/29/2008 1:32:00 AM

0

Patrick Doyle wrote:
>> Forgive me if I'm missing something obvious, but wouldn't it be a whole
>> lot more logical to do this?
>>
>> if x.is_a? Array
>> ...
>> end
> yup... that makes good sense too.
>
> This is a great night! :-)
>
> --wpd

Even better, if its possible you'd want to use the method on a structure
other than an array in the future, use 'respond-to?' instead. So for
instance, if you're protecting a call to 'each':

if x.respond_to? :each
x.each do
...
end
end

That way, it'll work not only with Arrays, but also with Lists and
Hashes or any other Enumerable class (or any class that implements
each). So if you later on decide that it would make more sense to store
your data in a List instead of an Array, if you use 'is_a?' you'll have
to go through and change your code every time you do this check, but if
you use 'respond_to?', you won't have to change a single line!

Of course, there are situations where 'is_a?' is necessary, but
'respond_to?' is generally preferable, from what I've seen.
--
Posted via http://www.ruby-....

Doug Glidden

8/29/2008 1:40:00 AM

0

Doug Glidden wrote:
[snip]
> That way, it'll work not only with Arrays, but also with Lists and
> Hashes or any other Enumerable class (or any class that implements
> each). So if you later on decide that it would make more sense to store
> your data in a List instead of an Array, if you use 'is_a?' you'll have
> to go through and change your code every time you do this check, but if
> you use 'respond_to?', you won't have to change a single line!
[snip]

Oops, I think that might have been a bad exampleâ??I don't think Ruby's
List class is actually the data structure known as a listâ??in any case,
it doesn't include Enumerable. Got my head in Java there for a second,
but you get the idea. There are LinkedList classes out there, though
(http://www.koders.com/ruby/fidCB5A463AB556F4FFD20E03139FF59563B118D108.asp...
for example).

- Doug
--
Posted via http://www.ruby-....