问题
I recently discovered that ls
in pry can take an argument like so: ls -l
.
My initial question is what the -l
part actually is - it is clearly not a string or symbol, and there is no local variable or method l
defined, so is there something else going on behind the scenes?
As an extension to my question, is ls
just a "normal" Ruby method defined by pry, or does it behave slightly differently?
I also noted that you get a different output if you pass a string (ls 'l'
) or symbol (ls :l
). Is there a full reference of the possible options?
回答1:
Passing -l
works as the whole line is evaluated as a string by the pry_eval
method. From that it matches the beginning against an existing command and extracts the rest as options to be passed in. From the Pry documentation:
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
You can see the full list of options by running ls -h
. This will return:
-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.
来源:https://stackoverflow.com/questions/58878992/how-is-the-ls-command-in-pry-able-to-accept-l-as-an-argument