ruby - Pry 中的 ls 命令如何接受 -l 作为参数?

标签 ruby pry pry-rails

我最近发现 pry 中的 ls 可以采用如下参数:ls -l

我最初的问题是 -l 部分实际上是什么 - 它显然不是字符串或符号,并且没有定义局部变量或方法 l ,所以幕后还有其他事情发生吗?

作为我的问题的扩展,ls 只是 pry 定义的“正常”Ruby 方法,还是它的行为略有不同?

我还注意到,如果传递字符串 (ls 'l') 或符号 (ls :l),您会得到不同的输出。是否有可能选项的完整引用?

最佳答案

传递 -l 的作用是通过 pry_eval 方法将整行计算为字符串。从这里开始,它将开头与现有命令进行匹配,并提取其余部分作为要传入的选项。来自 Pry 文档:

Nearly every piece of functionality in a Pry session is implemented as a command. Commands are not methods and must start at the beginning of a line, with no whitespace in between. Commands support a flexible syntax and allow 'options' in the same way as shell commands

您可以通过运行ls -h来查看完整的选项列表。这将返回:

-m, --methods               Show public methods defined on the Object (default)
-M, --instance-methods      Show methods defined in a Module or Class
-p, --ppp                   Show public, protected (in yellow) and private (in green) methods
-q, --quiet                 Show only methods defined on object.singleton_class and object.class
-v, --verbose               Show methods and constants on all super-classes (ignores Pry.config.ls.ceiling)
-g, --globals               Show global variables, including those builtin to Ruby (in cyan)
-l, --locals                Show locals, including those provided by Pry (in red)
-c, --constants             Show constants, highlighting classes (in blue), and exceptions (in purple)
-i, --ivars                 Show instance variables (in blue) and class variables (in bright blue)
-G, --grep                  Filter output by regular expression
-h, --help                  Show this message.

关于ruby - Pry 中的 ls 命令如何接受 -l 作为参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58878992/

相关文章:

ruby - 解析 'page 1 of x' - 最好的方法(ruby/mechanize/nokogiri)

MySQL、Rails ActiveRecord 日期分组和时区

ruby - 开发Ruby gem时如何添加 "pry"

ruby-on-rails - 无法使用 Pry-rails 逃避 eval

ruby-on-rails - Heroku 上的 Rails、Sidekiq、Redis——有问题

ruby-on-rails - RSpec/Shoulda : "should be_valid" fails, "contact.should be_valid"通过

ruby - 在 pry 中导航多行命令历史记录的奇怪错误

ruby - Socket 是否与 Pry 捆绑在一起?

ruby-on-rails - 使用 Rails 控制台时 pry-stack_explorer 中的 NoMethodError

ruby - 尝试加载 gem 'pry-rails' 时出错