Accept sh heredocs to Ruby script

  • Thread starter Oliver Saunders
  • Start date
O

Oliver Saunders

In sh, you can do this:

$ cat <<donenow
i
like
jam
donenow
i
like
jam
$

This suggests to me that cat should receive a temporary file as a result
of the heredoc that it can then display similar to the <() construct:

$ cat <(echo 'pie')
pie
$ echo <(echo 'pie')
/dev/fd/63

So I created a Ruby script called args_test.rb that looks like this:

#!/usr/bin/env ruby
p ARGV

Here it is run with a <() construct.

$ ./args_test.rb <(echo 'pie')
["/dev/fd/63"]

All good. But with heredoc you get this:

$ ./args_test.rb <<donenow
i
like
jam
donenow
[]

Can anyone explain this?
I tried this in Io with the same results.
 
K

KM

$ ./args_test.rb <<donenow
i
like
jam
donenow
[]

Can anyone explain this?

The script args_test.rb sees no command line arguments because the shell
didn't give it any. Instead,

This type of redirection instructs the shell to read input from the
current source until a line containing only WORD (with no trailing
blanks) is seen. All of the lines read up to that point are then used
as the standard input for a command.

- from the bash docs about here documents (not specific to bash though).
The opinion on jam was still available to the script on its standard
input, but nothing as an argument.
 
J

Joel VanderWerf

Oliver said:
In sh, you can do this:

$ cat <<donenow
i
like
jam
$

This suggests to me that cat should receive a temporary file as a result
of the heredoc that it can then display similar to the <() construct:

What leads you to think that << creates a temp file? Maybe the shell is
just piping the heredoc contents to the ruby child process's stdin?
 
F

Fabian Streitel

[Note: parts of this message were removed to make it a legal post.]
What leads you to think that << creates a temp file? Maybe the shell is
just piping the heredoc contents to the ruby child process's stdin?


you're right:

ruby -e"puts STDIN.gets" <<XYZ
bar
goo
XYZ
bar

Greetz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top