9

有没有一种快速的方法来找出哪些变量适用于当前范围,以及 Ruby on Rails 中哪个对象“self”指的是?

4

3 回答 3

34

我认为这pry是一个很好的解决方案,尽管debugger正如@jvnill 建议的那样,也可以。

gem install pry # or add to bundler

在您的代码中,您需要在有兴趣检查范围的任何地方添加以下内容:

require 'pry'
binding.pry # this will open a console view with the binding as the current scope

里面有一些东西可以满足pry你的要求:

pry> ls

ls将显示可以调用的变量和方法以及它们源自哪些对象/类。

pry> self # will return self in the current context
pry> pry-backtrace # shows the current stack
pry> help # will show the list of commands
pry> cd <some obj> # will change the scope to the target object
pry> exit # move back out to previous console scope

澄清您是否正在寻找完全不同的东西。

于 2013-02-25T01:49:49.097 回答
7

使用 gem “pry”,在 pry 上下文中发出命令 “ls -l” 以获取局部变量列表(以及变量中的值)。

于 2017-07-03T09:46:17.257 回答
2

我真的很喜欢使用 Better Errors gem,然后我只需在需要的地方“提出@somevar”,这样我就可以使用浏览器控制台并用棍子戳它。:) 上帝,我喜欢那个东西。

于 2013-02-25T01:30:22.960 回答