ruby-on-rails - 如何在 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

最佳答案

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

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

其中 my_string 是我的项目的名称

https://stackoverflow.com/questions/62357739/

相关文章:

node.js - Node http.大文件传输失败并显示 'ERR_STREAM_PREMATU

javascript - react-router-dom 不工作只是渲染 "/"

flutter - 如何在 Flutter 的 initState 中获取当前的 App Local

python - 如何在 Excel 的 Power Query 中运行 Python 脚本

javascript - 为什么我的 JavaScript 在使用 Blazor 时不能正确呈现?

linux - tar: 目录: 无法 rmdir: 目录不为空

python - `join` 或 `format` 字符串哪个更好?

c# - 使用 IIS 时出现多个错误

python-3.x - pynput pip3 安装错误 : Could not find a v

html - Nodejs如何从ejs文件生成pdf文件