Isn't t violation of codes? | Sololearn: Learn to code for FREE!


Isn't t violation of codes?

x = gets.to_i if x > 5 puts "wrong" elsif x <= 5 puts "right" else puts "error" end it works just fine when i enter numbers, i expected t would give error when i enter a string, but t puts right...why, why?

4/20/2017 11:14:29 AM

Kelvine De Danielo

2 Answers

New Answer


different languages have different output methods, 'puts' in C++ signifies for 'Output of string', C++ Is basic language,Complex languages like python and Ruby has different output method, you need to follow that to output,


The "to_i" method also accepts strings as input: The link above explains what the method does when it finds a string: > If there is not a valid number at the start of str, 0 is returned. I changed your code a bit to show exactly what to_i is doing, as shown here: If you input "A", then x becomes 0, which is <= 5, and therefore "right" according to your code.