首页 > 解决方案 > 如何在 ruby​​ 中找到调用者类?

问题描述

class A
  def bar
    B.new.foo
  end
end

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

A.new.bar

在里面B#foo,我怎样才能找到class A或任何其他引用或类名。基本上它是从哪个类调用的。

我试过这样的事情:

self.class
#B
self.class.superclass 
#Object

标签: ruby-on-railsrubyclassmodule

解决方案


为了获得整个堆栈,我通常会这样做:

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

my_string我的项目名称在哪里


推荐阅读