【问题标题】:How is the ls command in Pry able to accept -l as an argument?Pry 中的 ls 命令如何能够接受 -l 作为参数?
【发布时间】:2019-11-15 14:24:58
【问题描述】:

我最近发现ls in pry 可以接受如下参数:ls -l

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

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

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

【问题讨论】:

    标签: ruby pry pry-rails


    【解决方案1】:

    传递-l 可以通过pry_eval 方法将整行评估为字符串。从那里,它将开头与现有命令匹配,并将其余部分提取为要传递的选项。来自 Pry 文档:

    几乎每个 Pry 会话中的功能都实现为 一个命令。命令不是方法,必须从 一行,中间没有空格。命令支持灵活 语法并允许“选项”的方式与 shell 命令相同

    您可以通过运行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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-25
      • 2016-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 2011-01-15
      相关资源
      最近更新 更多