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

问题描述 投票:0回答:1

[我最近发现,撬中的ls可以接受这样的参数:ls -l

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

作为对我的问题的扩展,ls仅仅是pry定义的“常规” Ruby方法,还是其行为略有不同?

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

ruby pry pry-rails
1个回答
0
投票

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

Pry会话中几乎所有功能都实现为命令。命令不是方法,必须在命令开头一条线,中间没有空格。命令支持灵活语法,并以与shell命令相同的方式允许'options']

您可以通过运行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.
© www.soinside.com 2019 - 2024. All rights reserved.