How to find the caller class in ruby?

Viewed 471
class A
  def bar
    B.new.foo
  end
end

class B
  def foo
    #Here
    "HELLO WORLD!"
  end
end

A.new.bar

Inside B#foo, how can I find the class A or any other reference or class name. Basically from which class it was called.

I tried something like this:

self.class
#B
self.class.superclass 
#Object
1 Answers

To get the whole stack I usually do:

caller.select {|x| x.include?("my_string") }

where my_string is the name of my project

Related