Having problem comparing two predefined values in control flow


Here is the code which i wanted to execute but continously getting errors. Any help would be appreciated.

puts "Number please:"
num = gets.chomp
mun = 100
if num < mun
 puts "#{num}"
  puts "#{mun}"


What happens when you run the code? What do you expect to happen?


Following is returned when I try to run this code.
F:\Ruby\Ruby24-x64\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) C:/Users/Vishu/.RubyMine2017.2/config/scratches/scratch.rb
Number please:
C:/Users/Vishu/.RubyMine2017.2/config/scratches/scratch.rb:4:in <': comparison of String with 100 failed (ArgumentError) from C:/Users/Vishu/.RubyMine2017.2/config/scratches/scratch.rb:4:in<top (required)>'
from -e:1:in load' from -e:1:in

Process finished with exit code 1

But my expectation was that it should return the variable num if I enter a number smaller than 100.


num (the user input) is of type string, mun is of type integer

comparing this two different data types, bad idea. Maybe convert the user input to integer?


Yesterday I had the idea to make a program in ruby that can provide the mathematical tables for children using user input i.e…any number . So I wanted to do so using combination of control flow and loops. Can you help me building such programs. Any help would be appreciated.


That is a different question entirely. I haven’t programmed in ruby in ages, but you need to convert user input to integer:

num = gets.chomp
num = num.to_i

then a simple for loop:

for i in 0..10:
    puts i * num

to display the multiplication table.


Thanks for Help. Contact You soon in case of any query.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.