James Gray
1/21/2007 8:28:00 PM
On Jan 21, 2007, at 12:48 AM, Peter Szinek wrote:
> Nick,
>
>> number = gets
>
> after this line, try to
>
> p number
>
> to see what does it contain. It contains
>
> "4\n"
>
> So, there is an ugly "\n" (carriage return) at the end. Let's
> remove that!
>
> number = gets.chop
> p number
When you want to remove a line ending, prefer chomp() to chop().
chomp() will only target the line ending, which will cause you less
trouble when using it on a line that doesn't actually have one:
>> "James".chomp
=> "James"
>> "James".chop
=> "Jame"
Finally, neither is needed in this case:
>> "42\n".chomp.to_i == "42\n".to_i
=> true
James Edward Gray II