Read input from console in Ruby?

前端 未结 5 653
孤独总比滥情好
孤独总比滥情好 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:23

    you can also pass the parameters through the command line. Command line arguments are stores in the array ARGV. so ARGV[0] is the first number and ARGV[1] the second number

    #!/usr/bin/ruby
    
    first_number = ARGV[0].to_i
    second_number = ARGV[1].to_i
    
    puts first_number + second_number
    

    and you call it like this

    % ./plus.rb 5 6
    ==> 11
    

提交回复
热议问题