在pry中执行带有前导句点的语句

时间:2016-01-13 04:21:12

标签: ruby read-eval-print-loop irb pry

Pry将带有前导.的命令解释为系统命令,并将其传递给shell。但是,这使我无法使用具有前导.的行执行代码块,这在我的代码中非常频繁。

在IRB中,下面的内容工作正常:

2.2.3 :001 > begin
2.2.3 :002 >     "hello world"
2.2.3 :003?>       .split(" ")
2.2.3 :004?>       .map(&:upcase)
2.2.3 :005?>   end
 => ["HELLO", "WORLD"]
然而,在Pry中:

[6] pry(#<MailFetcherService>)> begin
[6] pry(#<MailFetcherService>)*   "hello world"
[6] pry(#<MailFetcherService>)*   .split(" ")
sh: -c: line 0: syntax error near unexpected token `" "'
sh: -c: line 0: `split(" ")'
Error: there was a problem executing system command: split(" ")

有办法解决这个问题吗?我对以下任何一个都没问题:

  • 仅在前面没有前导空格时将.解释为系统命令。
  • 更改为其他一些模糊分隔符,而不是.用于系统命令
  • 最后的手段:完全禁用系统命令 - 这不是我经常使用的功能。

1 个答案:

答案 0 :(得分:0)

Pry's internal wiki所述,您有两种选择:

  1. 你可以在命令前加上分号,这样Pry就会忽略它(在我看来很乱)。
  2. 您可以更改config.command_prefix属性(默认为空字符串""),这样就可以在需要使用系统命令时添加新的command_prefix。 / LI>

    #2的例子:

    pry(main)> Pry.config.command_prefix = "%"
    => "%"
    pry(main)> ls -l
    NameError: undefined local variable or method `l' for main:Object
    from (pry):2:in `<main>'
    pry(main)> %ls
    locals: _  _ex_  _pry_  _in_  _out_  _file_  _dir_
    pry(main)>