Tim Hunter
1/23/2007 1:22:00 AM
Lincoln Anderson wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> MikeGee wrote:
>
>> Michael W. Ryder wrote:
>>
>>> Is there any reason I cannot embed tabs in a string while using irb? I
>>> am trying to create a string using tabs to separate the various values
>>> for testing -- i.e. a = '1 2 3 4'. Using irb it either ignores the tab
>>> character or places a random value in the string.
>>> On a related note why is the value for the above string '1' if I use
>>> a.to_i to display it? I would have expected an error.
>>>
>> a = "1\t2\t3\t4"
>>
>>
>>
>>
>
> Actually, I tried this and it gives a string literal back
>
> =>"1\t2\t3\t4"
>
> This occurs as well if inserted into a full ruby script (not just irb).
>
Remember irb uses Kernel#p to echo the expression values. Try
a = "1\t2\t3\t4"
puts a
Also, String#to_ is working as designed. The ri documentation is quite
explicit.
$ ri String.to_i
------------------------------------------------------------ String#to_i
str.to_i(base=10) => integer
------------------------------------------------------------------------
Returns the result of interpreting leading characters in str as an
integer base base (2, 8, 10, or 16). Extraneous characters past
the end of a valid number are ignored. If there is not a valid
number at the start of str, 0 is returned. This method never
raises an exception.
"12345".to_i #=> 12345
"99 red balloons".to_i #=> 99