Meta: who is the reciever?

Hi guys,
first sorry my poor english. I’m a spanish speaker from argentina
seing a video-conf from Dave T. 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

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

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs