[Solved] Length of input characters in Ruby


Why they are different ?

It’s because in the second example there is a newline symbol also counted \n. Check it:

print "Your age:"
age=gets
print age
"33\n"
print age.chomp.length #  without newline
#> 2
print age.length #  with newline
#> 3

chomp

Returns a new String with the given record separator removed from the
end of str (if present).

gets

…. The separator is included with the contents of each record….

I advise you to read first the documentation for the methods what are you using. It’s much more better for education, than post a question and wait an answer.

2

solved Length of input characters in Ruby