Number multiplication in Ruby returning string concatenation instead of integer product -
the following code double number not working expected. e.g., if input 5, returning 55 instead of 10.
# program double number taken user input using user defined method def double (x) puts("lets yield!") yield x * 2 end puts "enter number want double : " x = gets.chomp double (x) { |n| puts n }
because using *
method on string. convert x
integer.
x = gets.chomp.to_i
Comments
Post a Comment