Read input from console in Ruby?

前端 未结 5 636
孤独总比滥情好
孤独总比滥情好 2020-12-23 02:50

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

5条回答
  •  失恋的感觉
    2020-12-23 03:18

    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

提交回复
热议问题