DEV Community

es404020
es404020

Posted on

Working with string in Ruby (part 2)

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}"

Enter fullscreen mode Exit fullscreen mode

let look at another example


puts "multiply any number by 2"
num =gets.chomp
puts num * 2

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Discussion (0)