[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Error in ancestor?

Robert Dober

6/16/2007 1:56:00 PM

Hi list

I just observed this (and it cost me quite some effort to debug my code :( )

515/15 > ruby -ve 'class << Class::new; puts self; puts ancestors.inspect end'
ruby 1.8.5 (2006-12-04 patchlevel 2) [i686-linux]
#<Class:#<Class:0xb7dfae50>>
[Class, Module, Object, Kernel]


this seems to be in contradiction with
http://www.ruby-doc.org/core/classes/Module.ht...
stating
-----------------------------------------------------------------------------
mod.ancestors ? array

Returns a list of modules included in mod (including mod itself).
=============

module Mod
include Math
include Comparable
end

Mod.ancestors #=> [Mod, Comparable, Math]
Math.ancestors #=> [Math]
---------------------------------------------------------------------------

Is this an error in doc or in behavior?

Cheers
Robert


--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

38 Answers

John Joyce

6/16/2007 2:25:00 PM

0


On Jun 16, 2007, at 8:56 AM, Robert Dober wrote:

> Hi list
>
> I just observed this (and it cost me quite some effort to debug my
> code :( )
>
> 515/15 > ruby -ve 'class << Class::new; puts self; puts
> ancestors.inspect end'
> ruby 1.8.5 (2006-12-04 patchlevel 2) [i686-linux]
> #<Class:#<Class:0xb7dfae50>>
> [Class, Module, Object, Kernel]
>
>
> this seems to be in contradiction with
> http://www.ruby-doc.org/core/classes/Module.ht...
> stating
> ----------------------------------------------------------------------
> -------
> mod.ancestors ? array
>
> Returns a list of modules included in mod (including mod itself).
>
> =============
>
> module Mod
> include Math
> include Comparable
> end
>
> Mod.ancestors #=> [Mod, Comparable, Math]
> Math.ancestors #=> [Math]
> ----------------------------------------------------------------------
> -----
>
> Is this an error in doc or in behavior?
>
> Cheers
> Robert
>
>
> --
> You see things; and you say Why?
> But I dream things that never were; and I say Why not?
> -- George Bernard Shaw
>
What error?
It works as described ModuleName.ancestors returns an array
including itself and its ancestors.

irb(main):034:0> Array.ancestors
=> [Array, Enumerable, Object, Kernel]
irb(main):035:0> Class.ancestors
=> [Class, Module, Object, Kernel]

Perhaps it does blur the lines of the term module
Module
Mod
Module is the Class Name that recent versions of Ruby respond to, Mod
is apparently an old synonym that is still in the Rdoc, but Ruby
doesn't recognize it, it could be a mistake in the docs, but it could
also be an old alias...

irb(main):036:0> Mod.ancestors
NameError: uninitialized constant Mod
from (irb):36
from :0
irb(main):037:0> Module.ancestors
=> [Module, Object, Kernel]


Robert Dober

6/16/2007 3:43:00 PM

0

On 6/16/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:
>
> On Jun 16, 2007, at 8:56 AM, Robert Dober wrote:
>
> > Hi list
> >
> > I just observed this (and it cost me quite some effort to debug my
> > code :( )
> >
> > 515/15 > ruby -ve 'class << Class::new; puts self; puts
> > ancestors.inspect end'
> > ruby 1.8.5 (2006-12-04 patchlevel 2) [i686-linux]
> > #<Class:#<Class:0xb7dfae50>>
> > [Class, Module, Object, Kernel]
> >
> >
> > this seems to be in contradiction with
> > http://www.ruby-doc.org/core/classes/Module.ht...
> > stating
> > ----------------------------------------------------------------------
> > -------
> > mod.ancestors ? array
> >
> > Returns a list of modules included in mod (including mod itself).
> >
> > =============
> >
> > module Mod
> > include Math
> > include Comparable
> > end
> >
> > Mod.ancestors #=> [Mod, Comparable, Math]
> > Math.ancestors #=> [Math]
> > ----------------------------------------------------------------------
> > -----
> >
> > Is this an error in doc or in behavior?
> >
> > Cheers
> > Robert
> >
> >
> > --
> > You see things; and you say Why?
> > But I dream things that never were; and I say Why not?
> > -- George Bernard Shaw
> >
> What error?
This error
ruby -ve 'class << Class::new; puts ancestors.include?( self ) end'
ruby 1.8.5 (2006-12-04 patchlevel 2) [i686-linux]
false
Robert

Morton Goldberg

6/16/2007 4:53:00 PM

0

On Jun 16, 2007, at 9:56 AM, Robert Dober wrote:

> Hi list
>
> I just observed this (and it cost me quite some effort to debug my
> code :( )
>
> 515/15 > ruby -ve 'class << Class::new; puts self; puts
> ancestors.inspect end'
> ruby 1.8.5 (2006-12-04 patchlevel 2) [i686-linux]
> #<Class:#<Class:0xb7dfae50>>
> [Class, Module, Object, Kernel]
>
>
> this seems to be in contradiction with
> http://www.ruby-doc.org/core/classes/Module.ht...
> stating
> ----------------------------------------------------------------------
> -------
> mod.ancestors ? array
>
> Returns a list of modules included in mod (including mod itself).
>
> =============
>
> module Mod
> include Math
> include Comparable
> end
>
> Mod.ancestors #=> [Mod, Comparable, Math]
> Math.ancestors #=> [Math]
> ----------------------------------------------------------------------
> -----
>
> Is this an error in doc or in behavior?

It cost me some effort to figure out what the question really
was :), but I presume you're wondering why the singleton class
doesn't appear in the ancestor list. I don't know the answer, but I
think the question is posed more clearly when the example is
constructed for the singleton class of an ordinary object.

<code>
class Foo; end
Bar = class << Foo.new; self; end
Bar # => #<Class:#<Foo:0x20e18>>
Bar.ancestors # => [Foo, Object, Kernel]
</code>

If I had to make a call, I would say that the documentation had
inadvertently omitted a very special case.

Regards, Morton

P.S. Why 'puts ancestors.inspect' instead of the simpler 'p ancestors'?

John Joyce

6/16/2007 6:35:00 PM

0


On Jun 16, 2007, at 10:43 AM, Robert Dober wrote:

> On 6/16/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:
>>
>> On Jun 16, 2007, at 8:56 AM, Robert Dober wrote:
>>
>> > Hi list
>> >
>> > I just observed this (and it cost me quite some effort to debug my
>> > code :( )
>> >
>> > 515/15 > ruby -ve 'class << Class::new; puts self; puts
>> > ancestors.inspect end'
>> > ruby 1.8.5 (2006-12-04 patchlevel 2) [i686-linux]
>> > #<Class:#<Class:0xb7dfae50>>
>> > [Class, Module, Object, Kernel]
>> >
>> >
>> > this seems to be in contradiction with
>> > http://www.ruby-doc.org/core/classes/Module.ht...
>> > stating
>> >
>> ---------------------------------------------------------------------
>> -
>> > -------
>> > mod.ancestors ? array
>> >
>> > Returns a list of modules included in mod (including mod itself).
>> >
>> > =============
>> >
>> > module Mod
>> > include Math
>> > include Comparable
>> > end
>> >
>> > Mod.ancestors #=> [Mod, Comparable, Math]
>> > Math.ancestors #=> [Math]
>> >
>> ---------------------------------------------------------------------
>> -
>> > -----
>> >
>> > Is this an error in doc or in behavior?
>> >
>> > Cheers
>> > Robert
>> >
>> >
>> > --
>> > You see things; and you say Why?
>> > But I dream things that never were; and I say Why not?
>> > -- George Bernard Shaw
>> >
>> What error?
> This error
> ruby -ve 'class << Class::new; puts ancestors.include?( self ) end'
> ruby 1.8.5 (2006-12-04 patchlevel 2) [i686-linux]
> false
> Robert
>
If you're expecting it to return true, it can't. Everything in the
array is an instance, not the class itself.

Robert Dober

6/16/2007 6:37:00 PM

0

On 6/16/07, Morton Goldberg <m_goldberg@ameritech.net> wrote:
> On Jun 16, 2007, at 9:56 AM, Robert Dober wrote:
>
> > Hi list
> >
> > I just observed this (and it cost me quite some effort to debug my
> > code :( )
> >
> > 515/15 > ruby -ve 'class << Class::new; puts self; puts
> > ancestors.inspect end'
> > ruby 1.8.5 (2006-12-04 patchlevel 2) [i686-linux]
> > #<Class:#<Class:0xb7dfae50>>
> > [Class, Module, Object, Kernel]
> >
> >
> > this seems to be in contradiction with
> > http://www.ruby-doc.org/core/classes/Module.ht...
> > stating
> > ----------------------------------------------------------------------
> > -------
> > mod.ancestors ? array
> >
> > Returns a list of modules included in mod (including mod itself).
> >
> > =============
> >
> > module Mod
> > include Math
> > include Comparable
> > end
> >
> > Mod.ancestors #=> [Mod, Comparable, Math]
> > Math.ancestors #=> [Math]
> > ----------------------------------------------------------------------
> > -----
> >
> > Is this an error in doc or in behavior?
>
> It cost me some effort to figure out what the question really
> was :), but I presume you're wondering why the singleton class

I know in ruby I would say
assert didactic_skills.zero? # :(

> doesn't appear in the ancestor list. I don't know the answer, but I
> think the question is posed more clearly when the example is
> constructed for the singleton class of an ordinary object.
>
> <code>
> class Foo; end
> Bar = class << Foo.new; self; end
> Bar # => #<Class:#<Foo:0x20e18>>
> Bar.ancestors # => [Foo, Object, Kernel]
> </code>

Sure thanks for adding this
>
> If I had to make a call, I would say that the documentation had
> inadvertently omitted a very special case.
Very probable given the code in class.c
>
> Regards, Morton
>
> P.S. Why 'puts ancestors.inspect' instead of the simpler 'p ancestors'?

Because output is shorter and more readable. I often get bitten by
debugging output
puts ary1
puts ary2
### Where the heck does ary2 start?

so I developed a reflex to use inspect for all kind of
debugging/demonstrating output.
I feel it is goo[df] practice.

Cheers
Robert
>
>


--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

Robert Dober

6/16/2007 7:46:00 PM

0

On 6/16/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:

> If you're expecting it to return true, it can't. Everything in the
> array is an instance, not the class itself.
>
>
Getting bitten by my didactic skills ;)
John please have a look at Morton's code, he was so kind to explain
what I am worried about much clearer.
That said maybe I can make things clearer myself:
irb(main):001:0> a = class A; ancestors end
=> [A, Object, Kernel]
irb(main):002:0> a.include? A
=> true
irb(main):003:0> B = Class::new
=> B
irb(main):004:0> Bsingle = class << B ; self end
=> #<Class:B>
irb(main):005:0> b = Bsingle.ancestors
=> [#<Class:B>, #<Class:Object>, Class, Module, Object, Kernel]
#Ah too bad my patched Ruby, I gotta recompile, sorry, normally the
singletons are #not in ancestor, please execute the code yourself to
see the difference.
irb(main):006:0> b.include? Bsingle
=> true

Cheers
Robert

--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

Morton Goldberg

6/16/2007 8:55:00 PM

0

On Jun 16, 2007, at 2:36 PM, Robert Dober wrote:

> On 6/16/07, Morton Goldberg <m_goldberg@ameritech.net> wrote:
>> P.S. Why 'puts ancestors.inspect' instead of the simpler 'p
>> ancestors'?
>
> Because output is shorter and more readable. I often get bitten by
> debugging output
> puts ary1
> puts ary2
> ### Where the heck does ary2 start?
>
> so I developed a reflex to use inspect for all kind of
> debugging/demonstrating output.
> I feel it is goo[df] practice.

But isn't 'p <expression>' shorthand for 'puts
(<expression>).inspect'? If this is the case and AFAIK it is, then
you just need to develop a new reflex to use p.

Regards, Morton

Robert Dober

6/16/2007 9:10:00 PM

0

On 6/16/07, Morton Goldberg <m_goldberg@ameritech.net> wrote:
> On Jun 16, 2007, at 2:36 PM, Robert Dober wrote:
>
> > On 6/16/07, Morton Goldberg <m_goldberg@ameritech.net> wrote:
> >> P.S. Why 'puts ancestors.inspect' instead of the simpler 'p
> >> ancestors'?
> >
> > Because output is shorter and more readable. I often get bitten by
> > debugging output
> > puts ary1
> > puts ary2
> > ### Where the heck does ary2 start?
> >
> > so I developed a reflex to use inspect for all kind of
> > debugging/demonstrating output.
> > I feel it is goo[df] practice.
>
> But isn't 'p <expression>' shorthand for 'puts
> (<expression>).inspect'? If this is the case and AFAIK it is, then
> you just need to develop a new reflex to use p.
Ah you said p, not puts, that's quite clever...
LOL you are an eagle eye, but you are right of course.
Robert
And yes you can chose the f above ;)

>
> Regards, Morton
>
>


--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

John Joyce

6/16/2007 10:38:00 PM

0


On Jun 16, 2007, at 4:09 PM, Robert Dober wrote:

> On 6/16/07, Morton Goldberg <m_goldberg@ameritech.net> wrote:
>> On Jun 16, 2007, at 2:36 PM, Robert Dober wrote:
>>
>> > On 6/16/07, Morton Goldberg <m_goldberg@ameritech.net> wrote:
>> >> P.S. Why 'puts ancestors.inspect' instead of the simpler 'p
>> >> ancestors'?
>> >
>> > Because output is shorter and more readable. I often get bitten by
>> > debugging output
>> > puts ary1
>> > puts ary2
>> > ### Where the heck does ary2 start?
>> >
>> > so I developed a reflex to use inspect for all kind of
>> > debugging/demonstrating output.
>> > I feel it is goo[df] practice.
>>
>> But isn't 'p <expression>' shorthand for 'puts
>> (<expression>).inspect'? If this is the case and AFAIK it is, then
>> you just need to develop a new reflex to use p.
> Ah you said p, not puts, that's quite clever...
> LOL you are an eagle eye, but you are right of course.
> Robert
> And yes you can chose the f above ;)
>
>>
>> Regards, Morton
>>
>>
>
>
> --
> You see things; and you say Why?
> But I dream things that never were; and I say Why not?
> -- George Bernard Shaw
>
Actually p is more like a short form for
puts object.inspect

Morton Goldberg

6/16/2007 11:53:00 PM

0

On Jun 16, 2007, at 6:37 PM, John Joyce wrote:

> Actually p is more like a short form for
> puts object.inspect

I admit that's more strictly accurate, but I find it very close to a
distinction without a difference since I can't think of a case where
(<expression>) doesn't evaluate to an object.

Regards, Morton