Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

class of function

0 views
Skip to first unread message

Daniel Schüle

unread,
Jul 8, 2005, 6:51:17 PM7/8/05
to
Hello

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

Joel VanderWerf

unread,
Jul 8, 2005, 7:36:02 PM7/8/05
to

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


Florian Groß

unread,
Jul 8, 2005, 7:38:31 PM7/8/05
to
Daniel Schüle wrote:

method(:y).class or method(:y).object_id

Charles Steinman

unread,
Jul 8, 2005, 7:56:34 PM7/8/05
to

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.

Logan Capaldo

unread,
Jul 8, 2005, 8:09:56 PM7/8/05
to

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.

Paul Brannan

unread,
Jul 14, 2005, 11:38:16 AM7/14/05
to
On Sat, Jul 09, 2005 at 08:00:47AM +0900, Daniel Sch?le wrote:
> how to get the class of the function y?
> or object_id for example?

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


0 new messages