mardi 12 janvier 2016

How to determine the class a method was defined in?

I would like to dynamically determine the class the current method was defined in.

Here's a static example of what I'm trying to do:

class A
  def foo
    puts "I was defined in A"
  end
end

class B < A
  def foo
    puts "I was defined in B"
    super
  end
end

A.new.foo
# I was defined in A

B.new.foo
# I was defined in B
# I was defined in A

How can I replace A and B in the strings above with a dynamic expression?

Apparently, #{self.class} does not work. (it would return I was defined in B twice for B)





Aucun commentaire:

Enregistrer un commentaire