[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

alias of some methods does not work

gga

7/17/2005 9:58:00 PM

irb(main):006:0> module Kernel
irb(main):007:1> alias :_methods :methods
irb(main):008:1> end

is all OK.

Yet:
irb(main):001:0> module Kernel
irb(main):002:1> alias :_instance_methods :instance_methods
irb(main):003:1> end
NameError: undefined method `instance_methods' for module `Kernel'
from (irb):2
from :0
irb(main):004:0> Kernel.instance_methods
=> ["freeze", "__id__", "singleton_methods", ...]

Is this by design or a bug? Why can't I alias any of the following:
# alias :_constants :constants
# alias :_instance_methods :instance_methods
# alias :_public_instance_methods :public_instance_methods
# alias :_protected_instance_methods :protected_instance_methods
# alias :_private_instance_methods :private_instance_methods
?

Reason I want to alias them is so that I can add some function into my
..irbrc so that those arrays get returned in sorted order automatically.

5 Answers

Zach Dennis

7/17/2005 10:12:00 PM

0

gga wrote:
> irb(main):006:0> module Kernel
> irb(main):007:1> alias :_methods :methods
> irb(main):008:1> end
>
> is all OK.
>
> Yet:
> irb(main):001:0> module Kernel
> irb(main):002:1> alias :_instance_methods :instance_methods
> irb(main):003:1> end
> NameError: undefined method `instance_methods' for module `Kernel'
> from (irb):2
> from :0
> irb(main):004:0> Kernel.instance_methods
> => ["freeze", "__id__", "singleton_methods", ...]
>
> Is this by design or a bug? Why can't I alias any of the following:
> # alias :_constants :constants
> # alias :_instance_methods :instance_methods
> # alias :_public_instance_methods :public_instance_methods
> # alias :_protected_instance_methods :protected_instance_methods
> # alias :_private_instance_methods :private_instance_methods
> ?

Because those don't belong to Kernel. They belong to the class Module...

irb(main):015:0> class Module
irb(main):016:1> alias :meths :instance_methods
irb(main):017:1> end
=> nil
irb(main):018:0> Module.meths
=> ["send", "name", "class_eval", "object_id", "...etc....

Zach


gga

7/18/2005 1:25:00 AM

0




> Because those don't belong to Kernel. They belong to the class Module...
>
> irb(main):015:0> class Module
> irb(main):016:1> alias :meths :instance_methods
> irb(main):017:1> end
> => nil
> irb(main):018:0> Module.meths
> => ["send", "name", "class_eval", "object_id", "...etc....
>
> Zach

Ok... but color me confused. Kernel is a module which has those
functions I could not alias. How does a module inherit functions from
a class? Also, why does Kernel.ancestors not show this relationship?
If I do Module.ancestors, it shows Kernel instead as one of its
ancestors. What kind of magic is going on behind the scenes here?

Devin Mullins

7/18/2005 1:47:00 AM

0

irb(main):001:0> Kernel.object_id
=> 20763780
irb(main):002:0> Kernel.class
=> Module

Kernel is a constant (like Math::PI) that is an instance of Module.
ancestors is an instance method of Module, so it is available on Kernel.
Methods like 'puts' for example are module methods on Kernel. Meh, I'm
too tired to explain better than that right now. :P Start Googling.

Devin

gga wrote:

>Ok... but color me confused. Kernel is a module which has those
>functions I could not alias. How does a module inherit functions from
>a class? Also, why does Kernel.ancestors not show this relationship?
>If I do Module.ancestors, it shows Kernel instead as one of its
>ancestors. What kind of magic is going on behind the scenes here?
>
>



dblack

7/18/2005 2:11:00 AM

0

daz

7/18/2005 2:32:00 AM

0


GGarramuno wrote:
>
> Ok... but color me confused. Kernel is a module which has those
> functions I could not alias. How does a module inherit functions from
> a class? Also, why does Kernel.ancestors not show this relationship?
> If I do Module.ancestors, it shows Kernel instead as one of its
> ancestors. What kind of magic is going on behind the scenes here?
>


class Object
class << self
p [:A, self, ancestors]
#-> [:A, #<Class:Object>, [Class, Module, Object, Kernel]]
alias :_constants :constants
alias :_instance_methods :instance_methods
alias :_public_instance_methods :public_instance_methods
alias :_protected_instance_methods :protected_instance_methods
alias :_private_instance_methods :private_instance_methods
end
p [:B, self, ancestors] #-> [:B, Object, [Object, Kernel]]
alias :_methods :methods
end

klass = String
p klass._constants == klass.constants
p klass._instance_methods == klass.instance_methods
p klass._public_instance_methods == klass.public_instance_methods
p klass._protected_instance_methods == klass.protected_instance_methods
p klass._private_instance_methods == klass.private_instance_methods
p klass.new._methods == klass.new.methods
#-> true


daz