快速查看适用于当前范围的变量以及自身引用的对象RAILS

Jor*_*ton 8 ruby-on-rails

有没有一种快速的方法可以找出哪些变量适用于当前作用域,哪些对象'self'在Ruby on Rails中引用?

Mat*_*sel 25

我认为这pry是一个很好的解决方案,但debugger正如@jvnill建议的那样,它也有效.

gem install pry # or add to bundler
Run Code Online (Sandbox Code Playgroud)

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

require 'pry'
binding.pry # this will open a console view with the binding as the current scope
Run Code Online (Sandbox Code Playgroud)

pry你所要求的内容中有一些东西:

pry> ls
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

澄清你是否在寻找完全不同的东西.


小智 6

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

  • 和`ls -i` 用于实例变量。 (3认同)

Pol*_*her 2

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