[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

class of function

Daniel Schüle

7/8/2005 10:51:00 PM

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

5 Answers

Joel VanderWerf

7/8/2005 11:36:00 PM

0

Daniel Schüle wrote:
> 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?
>

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ß

7/8/2005 11:39:00 PM

0

acharlieblue

7/8/2005 11:57:00 PM

0


Florian Groß wrote:
> Daniel Schüle wrote:
>
> > 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?
>
> 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.

Logan Capaldo

7/9/2005 12:10:00 AM

0


On Jul 8, 2005, at 7:38 PM, Florian Groß wrote:

> Daniel Schüle wrote:
>
>
>> 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?
>>
>
> method(:y).class or method(:y).object_id
>
>
>

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

7/14/2005 3:38:00 PM

0

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