0

I am using the system curl as I cannot get the ruby curl to work. How do I get the output of the curl into my ruby variable? the example below shows that the variable did not get the output of the curl

url1="\"https://ca.yahoo.com/\""
a=Kernel.system "curl #{url1}"
irb(main):026:0> a
=> true
junkone
  • 1,427
  • 1
  • 20
  • 45

1 Answers1

1

Kernel#system Doesn't Capture Standard Output

Kernel#system only returns true, false or nil, so you can't capture command output that way. Instead, use Kernel#` (or its alternative %x{} syntax) to capture standard output. For example:

url    = 'https://ca.yahoo.com/'
output = %x(curl "#{url}")

For more complex things, you may also want to look at the Open3 module from the Ruby Standard Library. It has various methods for supporting pipelines, and different ways to treat standard input, standard output, and standard error separately when necessary.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199