百分号 + 的含义是什么? Ruby 中的管道运算符,如“%|”?

发布于 2024-12-13 15:46:46 字数 949 浏览 0 评论 0原文

我试图理解 此网站:

#!/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_filepathio 呢?我还知道 基本 % 字符串操作,例如%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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不必了 2024-12-20 15:46:46

它是一个字符串。在 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.

梦萦几度 2024-12-20 15:46:46

这是字符串文字的另一个示例:

var = %|foobar|
var.class # => String

您可以使用任何单个非字母数字字符作为分隔符,如下所示:

var = %^foobar^
var.class # => String

This is another example of string literal:

var = %|foobar|
var.class # => String

You can use any single non-alpha-numeric character as the delimiter, like so:

var = %^foobar^
var.class # => String
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文