In Ruby, is there a way to get the name of the class that creates an instance of MyClass?
I know that I could pass it in as an argument on my initialize method, but I want to know if any data is already there that has to do with the class that created an instance of MyClass in side of MyClass.
So it would be like
class MyClass
def initialize
@who_called_me = who_called_me.name
end
def who_called_me
puts @who_called_me
end
end
Although this is not portable between implementations and versions, this is a crude solution:
What this does is it gets the current stack, skips the strings for
initialize,allocate, andnew, and then gets the method name out of the string. Again, this is a total hack, and is based around unspecified behavior. I would not suggest using this unless absolutely necessary.