Read input from console in Ruby?

Sergey picture Sergey · Jul 2, 2011 · Viewed 143.1k times · Source

I want to write a simple A+B program in ruby, but I have no idea how to work with the console.

Answer

siame picture siame · Jul 2, 2011

Are you talking about gets?

puts "Enter A"
a = gets.chomp
puts "Enter B"
b = gets.chomp
c = a.to_i + b.to_i
puts c

Something like that?

Update

Kernel.gets tries to read the params found in ARGV and only asks to console if not ARGV found. To force to read from console even if ARGV is not empty use STDIN.gets