0

How do I store Ruby standard outputs to multiple variables?

For example, if I have:

puts "hello"

puts "thanks"

How do I store "hello" and "thanks" to two different variables like strVar (containing the value "hello") and strVar2 (containing the value "thanks").

In my script, I am calling another Ruby script which will puts multiple strings to standard output. How do I store each string from the standard output individually?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
000000000000000000000
  • 780
  • 4
  • 15
  • 47
  • You're trying to capture the standard output of your own script in variables? – mu is too short Nov 30 '16 at 18:44
  • @muistooshort Sort of. In my script, I am calling another ruby script which will puts multiple strings to standard output. How do I store each string from the standard output individually? Thanks. – 000000000000000000000 Nov 30 '16 at 18:50
  • 1
    Although I wouldn't recommend it, [StringIO](http://ruby-doc.org/stdlib-1.9.3/libdoc/stringio/rdoc/StringIO.html) is the way to go. Take a look at [this answer](http://stackoverflow.com/a/14988271/1179430) – GAntoine Nov 30 '16 at 18:51
  • 1
    http://ruby-doc.org/stdlib-2.3.3/libdoc/open3/rdoc/Open3.html – mu is too short Nov 30 '16 at 19:12

2 Answers2

0

I'm not sure that I understand the question, but there are countless numbers of ways to store / print strings. Its hard to imagine a situation where you would have a value following puts that is not entered manually or set programatically.

You can save input variables with gets or $stdin.gets or as an argument with the ARGV array. For example:

puts "Enter the first string"
var0 = $stdin.gets.chomp

If you already have the values saved

var1 = "hello"
var2 = "thanks"
array = [var1, var2]
hash = {:key1 => var1, :key2 => var2}


puts var1
puts var2
array.each do |str| puts str end
hash.map do |k, v| puts v end
whodini9
  • 1,434
  • 13
  • 18
  • Thanks. But what if I don't have the values saved? For example, in my case, I call another script in my script so I have no control. The other scripts has many lines where strings are puts to standout output. – 000000000000000000000 Nov 30 '16 at 18:59
  • Maybe you could use pipes? For example, ruby script1.rb | ruby script2.rb and use $stdin.gets to get the output? I don't think there is a way to both execute another ruby script an and also monitor $stdin at the same time within script2. Also what should happen if script1 has an Exception? The proper way to do this is to use a database/redis/filestore and store the values there :) – whodini9 Nov 30 '16 at 19:04
  • Also this: http://stackoverflow.com/questions/14987362/how-can-i-capture-stdout-to-a-string – whodini9 Nov 30 '16 at 19:09
  • I think this part is consistent with what I have found so far. Thanks. :) – 000000000000000000000 Nov 30 '16 at 19:13
  • 1
    If you want more control use the [Open3](https://ruby-doc.org/stdlib-2.3.0/libdoc/open3/rdoc/Open3.html) library and a method like [`popen3`](https://ruby-doc.org/stdlib-2.3.0/libdoc/open3/rdoc/Open3.html#method-c-popen3) which gives you handles for input, output, errors and a way to wait for that process to finish. – tadman Nov 30 '16 at 20:28
  • @tadman Thank you sir. This is more advanced but works for me. – 000000000000000000000 Nov 30 '16 at 23:07
  • It isn't necessary to use `$stdin.gets` because, [by default `gets` reads from the standard input stream](http://ruby-doc.org/core-2.3.3/Kernel.html#method-i-gets), just as it's not necessary to use `$stdout.puts` since [`puts` defaults to the standard output stream](http://ruby-doc.org/core-2.3.3/Kernel.html#method-i-puts). – the Tin Man Dec 01 '16 at 17:08
0

You're basically chaining applications/scripts together. There are multiple ways to do it but the simplest path uses the STDIN/STDOUT pipeline.

A simple example would be using two small scripts. Save this as test.rb:

puts 'foo'
puts 'bar'

and this as test2.rb:

v1 = gets.chomp
v2 = gets.chomp

puts "v1=#{v1} v2=#{v2}"

then, at the command line use:

ruby test.rb | ruby test2.rb

which will output:

v1=foo v2=bar

| is how we chain the output of one script to the input of another and isn't part of Ruby, it's part of the OS.

This works because, by default, puts writes to STDOUT and gets reads from STDIN. | wires them together.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303