What is the value of the percent + sign + pipe operator in Ruby, as in "% |"?

I am 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 terminal_script_filepath function starts with:

 %| … … | 

... and where he "understands" in:

 { |io| io << terminal_script_filepath } 

What Ruby concepts are used here?

I know that open() with a channel helps me provide input to the STDIN of the process, but how is input from terminal_script_filepath to io introduced? I also know the basic % operations with strings , for example %w , but what does the pipe do here?

+7
source share
2 answers

This is a string. In ruby ​​you can define strings in possible ways. Single or double quotes are most common,% s is different. You can also define strings with any separator as used in this script. For example, %^Is also a string^ or %$Also a string$ . You just need not to use these characters inside the string.

In this case, << used as a concatenation operation, adding a line to the file, or in this case, the channel that AppleScript listens to.

+10
source

This is another example of a string literal:

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

You can use any single non-alphanumeric character as a separator:

 var = %^foobar^ var.class # => String 
+4
source

All Articles