百分号 + 的含义是什么? Ruby 中的管道运算符,如“%|”?
我试图理解 此网站:
#!/usr/bin/env ruby
require ENV['TM_SUPPORT_PATH'] + '/lib/escape.rb'
def terminal_script_filepath
%|tell application "Terminal"
activate
do script "jsc -i #{e_as(e_sh(ENV['TM_FILEPATH']))}"
end tell|
end
open("|osascript", "w") { |io| io << terminal_script_filepath }
最重要的是,函数 terminal_script_filepath
开头的部分是:
%| …
… |
... 以及它被“解析”的位置:
{ |io| io << terminal_script_filepath }
这里使用了 Ruby 的哪些概念?
我知道使用管道的 open()
可以帮助我将输入提供给进程的 STDIN,但是输入如何从 terminal_script_filepath
到 io
呢?我还知道 基本 %
字符串操作,例如%w
,但是管道在这里做什么呢?
I'm trying to understand the script presented on this site:
#!/usr/bin/env ruby
require ENV['TM_SUPPORT_PATH'] + '/lib/escape.rb'
def terminal_script_filepath
%|tell application "Terminal"
activate
do script "jsc -i #{e_as(e_sh(ENV['TM_FILEPATH']))}"
end tell|
end
open("|osascript", "w") { |io| io << terminal_script_filepath }
Most importantly, the part where the function terminal_script_filepath
begins with:
%| …
… |
… and where it is "parsed" in:
{ |io| io << terminal_script_filepath }
Which concepts of Ruby are used here?
I know that open()
with a pipe helps me feed input to the STDIN of a process, but how does the input get from terminal_script_filepath
to io
? I also know the basic %
operations with strings, like %w
, but what does the pipe do here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它是一个字符串。在 ruby 中,您可以通过多种方式定义字符串。单引号或双引号是最常见的,%s 是另一种。您还可以使用任何分隔符定义字符串,如此脚本中所使用的。例如
%^也是字符串^
,或者%$也是字符串$
。您只需确保不在字符串中使用这些字符即可。在本例中,
<<
用作串联操作,将字符串附加到文件,或者在本例中,将字符串附加到侦听 AppleScript 的管道。It is a string. In ruby, you can define strings in may ways. Single or double quotes are the most common, %s is another. You can also define strings with any delimiter, as used in this script. For example
%^Is also a string^
, or%$Also a string$
. You just have to make sure to not use those characters inside the string.The
<<
in this case is being used as a concatenation operation, appending the string to a file, or in this case, a pipe that listens to AppleScript.这是字符串文字的另一个示例:
您可以使用任何单个非字母数字字符作为分隔符,如下所示:
This is another example of string literal:
You can use any single non-alpha-numeric character as the delimiter, like so: