From: Ralph Shnelvar Date: 2012-03-07T02:35:00+09:00 Subject: Re: Getting the class of an object. Re[2]: Getting the class of an object.

ToMaTo,


The convention on this list is to bottom post.


Tuesday, March 6, 2012, 10:10:45 AM, you wrote:



Are you talking about sth. like this:


b.method(:foo).owner #=> Dance

b.method(:bar).owner #=> Boogy


?



On Tue, Mar 6, 2012 at 11:34 AM, Ralph Shnelvar <ralphs@dos32.com> wrote:


Consider;


- - -


class Dance

   def foo

       puts "foo is executed"

       42

   end

end


class Boogy < Dance

   def bar

       puts (foo.class)

   end

end


b = Boogy.new


b.foo # prints "foo is executed" ... expected.


b.bar # prints "foo is executed\nFixnum" ... not expected!


- - -


Let's focus on the line

 puts (foo.class)


So let's say I'm in the middle of a debugging session trying to debug the bar method.


I see this thing called "foo" and I want to know what it is.


So I

 puts (foo.class)


Since everything in ruby is an object and all objects have classes, I'm expecting to print out the class of this thing called foo.


What happens, though is that foo gets executed (which is not what I want) and returns 42 ... whose class is Fixnum.



Questions:

How can I tell what class of object foo is without executing it?

Is there a class called "Method" in the Ruby class hierarchy?

What class of object does define_method return?



Ralph Shnelvar







-- 


Best wishes,

Jie Fan

 

Life is a miracle.




TG>

TG> Are you talking about sth. like this:

TG>

TG> b.method(:foo).owner #=> Dance

TG> b.method(:bar).owner #=> Boogy

TG>

TG> ?




No.


Let's say I am examining the source code and I see "foo".  I don't know what it is.  Is it a method?  Is it a local variable?  Is it a proc passed in as something on the parameter list?  Is it a binding?


How can I find out what foo is without actually executing foo?