So in part 1 we looked at string manipulation. In this part 2 we focus on how to receive input and display.
To accept an input in ruby we use gets.chomp
puts "what your firstname?"
first_name =gets.chomp
puts "Your firstname is #{first_name}"
let look at another example
puts "multiply any number by 2"
num =gets.chomp
puts num * 2
This would create two instance of the number enter .If the user entered 5 result would be "55".This is because gets.chomp sees it as a string .To fix use
puts num.to_i * 2
Top comments (0)