[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

meta: who is the reciever?

Gabriel Pugliese

12/11/2007 10:22:00 PM

Hi guys,
first sorry my poor english. I'm a spanish speaker from argentina
seing a video-conf from Dave Thomas on metaprogramming I rose these
questions:
who is the reciever? where are stored the method definitions?
*************************************************
class Dave
class << self
def hi
puts 123
end
def self.hi
puts 456
end
end

def self.call_hi_low
hi
end

def self.call_hi_up
class << self
hi
end
end
end

Dave.call_hi_low #=>123
Dave.call_hi_up #=>456
--
Posted via http://www.ruby-....

3 Answers

bermonruf

12/12/2007 2:41:00 AM

0

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

Take a look:

class Object

# Just to access the metaclass
def metaclass
class << self; self; end
end

end

class Foo

class << self # Inside the metaclass

def a # A: class method of Foo or method of the Foo's metaclass
"Class method";
end

def self.b # B: class method of the metaclass, or method of the
metaclass of Foo's metaclass
"Class method of the metaclass";
end

end

def self.c # C: same as A
"Class method";
end

def d # D: instance method
"Instance method";
end

def self.e # E: yet another example of A and C
class << self # Inside the metaclass of Foo, here we can call methods
like B
b;
end
end

end

["Foo.c", "Foo.metaclass.b", "Foo.c", "Foo.new.d", "Foo.e"].each do |code|
puts "#{code} \#=> #{eval(code)}";
end

bermonruf

12/12/2007 2:42:00 AM

0

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

Will produce:

Foo.c #=> Class method
Foo.metaclass.b #=> Class method of the metaclass
Foo.c #=> Class method
Foo.new.d #=> Instance method
Foo.e #=> Class method of the metaclass

Gabriel Pugliese

12/12/2007 9:50:00 PM

0

Thanks Bernardo
still a bit confusing for me but you provided enough working material to
analyze
looks like a chain: a method of the metaclass of the Foo's metaclass


Bernardo Rufino wrote:
> Will produce:
>
> Foo.c #=> Class method
> Foo.metaclass.b #=> Class method of the metaclass
> Foo.c #=> Class method
> Foo.new.d #=> Instance method
> Foo.e #=> Class method of the metaclass

--
Posted via http://www.ruby-....