Monday, July 18, 2011

Read input from console in Ruby


puts "Enter A"
a
= gets.chomp
puts
"Enter B"
b
= gets.chomp
c
= Integer(a) + Integer(b)
puts c
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

No comments:

Post a Comment