irb(main):177:0* y=0
=> 0
irb(main):178:0> def y; 1; end
=> nil
irb(main):179:0> y
=> 0
irb(main):180:0> y()
=> 1
irb(main):181:0> y.class
=> Fixnum
irb(main):182:0>
how to get the class of the function y?
or object_id for example?
Regards, Daniel
You have defined a method #y in the "top-level object", which irb calls
"main". It's not an object itself.
irb(main):001:0> self
=> main
irb(main):002:0> self.methods.grep /y/
=> ["type", "display"]
irb(main):003:0> def y; 1; end
=> nil
irb(main):004:0> self.methods.grep /y/
=> ["type", "display", "y"]
method(:y).class or method(:y).object_id
Note, though, that repeated calls to method(:y) will return different
objects. It creates a Method object that represents to the method
self.y, but it doesn't return the method itself, which is not an object.
That's kind of a lie...(as I'm sure you know, and a relatively benign
one)
eg:
logan:/Users/logan% irb
irb(main):001:0> def y; 0; end
=> nil
irb(main):002:0> a = method(:y)
=> #<Method: Object#y>
irb(main):003:0> b = method(:y)
=> #<Method: Object#y>
irb(main):004:0> a.object_id
=> 1118516
irb(main):005:0> b.object_id
=> 1113486
If method(:y) was really the wall to get y's object_id those numbers
should be the same. In ruby methods aren't objects. you can use
methods like method to get procs whose sole purpose is to call them,
but y isn't really an object on its own. It might help to think of
someObject.some_message as not calling a function or method name some-
Message but trather that it is the syntax for sending a message named
some_message to someObject. Indeed the dot notation one could pretend
is syntatic sugar for someObject.send(:some_message). This also
makes sense in the context of method_missing.
Not sure exactly what you want, but using the latest nodewrap from cvs:
irb(main):001:0> def y; 1; end
=> nil
irb(main):002:0> method(:y).origin_class
=> Object
irb(main):003:0> class Foo; def y; end; end
=> nil
irb(main):004:0> Foo.new.method(:y).origin_class
=> Foo
Paul