class of function

D

Daniel Schüle

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
 
J

Joel VanderWerf

Daniel said:
Hello
=20
irb(main):177:0* y=3D0
=3D> 0
irb(main):178:0> def y; 1; end
=3D> nil
irb(main):179:0> y
=3D> 0
irb(main):180:0> y()
=3D> 1
irb(main):181:0> y.class
=3D> Fixnum
irb(main):182:0>
=20
how to get the class of the function y?
or object_id for example?

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
=3D> main
irb(main):002:0> self.methods.grep /y/
=3D> ["type", "display"]
irb(main):003:0> def y; 1; end
=3D> nil
irb(main):004:0> self.methods.grep /y/
=3D> ["type", "display", "y"]
 
F

Florian Groß

Daniel said:
irb(main):177:0* y=3D0
=3D> 0
irb(main):178:0> def y; 1; end
=3D> nil
irb(main):179:0> y
=3D> 0
irb(main):180:0> y()
=3D> 1
irb(main):181:0> y.class
=3D> Fixnum
irb(main):182:0>
=20
how to get the class of the function y?
or object_id for example?

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

Charles Steinman

Florian said:
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.
 
L

Logan Capaldo

Daniel Sch=FCle wrote:



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

That's kind of a lie...(as I'm sure you know, and a relatively benign =20=

one)

eg:

logan:/Users/logan% irb
irb(main):001:0> def y; 0; end
=3D> nil
irb(main):002:0> a =3D method:)y)
=3D> #<Method: Object#y>
irb(main):003:0> b =3D method:)y)
=3D> #<Method: Object#y>
irb(main):004:0> a.object_id
=3D> 1118516
irb(main):005:0> b.object_id
=3D> 1113486

If method:)y) was really the wall to get y's object_id those numbers =20
should be the same. In ruby methods aren't objects. you can use =20
methods like method to get procs whose sole purpose is to call them, =20
but y isn't really an object on its own. It might help to think of =20
someObject.some_message as not calling a function or method name some-=20=

Message but trather that it is the syntax for sending a message named =20=

some_message to someObject. Indeed the dot notation one could pretend =20=

is syntatic sugar for someObject.send:)some_message). This also =20
makes sense in the context of method_missing.=
 
P

Paul Brannan

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,944
Members
47,491
Latest member
mohitk

Latest Threads

Top