[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Placing tabs in strings in irb

Michael W. Ryder

1/23/2007 12:44:00 AM

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.
14 Answers

MikeGee

1/23/2007 12:48:00 AM

0

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"

Lincoln Anderson

1/23/2007 1:06:00 AM

0

-----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).

Lincoln Anderson
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail....

iD8DBQFFtV92R8wmeqHdtdcRAmyKAKCyTBb0pVFSCqGVD61LJjCRhg25ZwCdHH5O
ZTL//ymQ5UxGL+uP/gGfypQ=
=tdLS
-----END PGP SIGNATURE-----

WoNáDo

1/23/2007 1:19:00 AM

0

Lincoln Anderson schrieb:
> Actually, I tried this and it gives a string literal back
>
> =>"1\t2\t3\t4"

It works fine. "irb"'s output is after applying "inspect".

irb(main):001:0> a = "1\t2"
=> "1\t2"
irb(main):002:0> puts a
1 2
=> nil
irb(main):003:0> a.length
=> 3

Wolfgang Nádasi-Donner

Tim Hunter

1/23/2007 1:22:00 AM

0

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

Joel VanderWerf

1/23/2007 1:25:00 AM

0

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.

If you *want* this to error, use

irb(main):001:0> Integer("1 2")
ArgumentError: invalid value for Integer: "1 2"
from (irb):4:in `Integer'
from (irb):4

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Lincoln Anderson

1/23/2007 1:26:00 AM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Nathan Smith wrote:
>>
>> 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).
>>
>> Lincoln Anderson
>
>
> In irb if you do a="1\t2\t3\t4" it replies with "1\t2\t3\t4". But, if
> you do
> puts a, it gives you: 1 2 3 4.
>
> The string must be in double quotes for this to work.
>
>> 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.
>
> Extraneous characters past the end of a valid number are ignored, according
> to the docs.
>
> Nate
>

Okay, shoulda guessed that the "output" from defining a wouldn't be
formatted. And when I check my ruby script I see that I had it single
quoted, not double-quoted. Cool, I learneded something today.

Lincoln Anderson
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail....

iD8DBQFFtWQqR8wmeqHdtdcRAgt1AJsHk6tqx8O53aejXnKon5QLWQXSAgCfQ4kg
h1hKQ4B+SA44EqchgM6gW+w=
=oJNw
-----END PGP SIGNATURE-----

Michael W. Ryder

1/23/2007 1:54:00 AM

0

Joel VanderWerf 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.
>
> If you *want* this to error, use
>
> irb(main):001:0> Integer("1 2")
> ArgumentError: invalid value for Integer: "1 2"
> from (irb):4:in `Integer'
> from (irb):4
>
Thanks for the tip. I had known about the to_i method and assumed that
was the only way to do the conversion.

George

1/23/2007 1:58:00 AM

0

On 1/23/07, Michael W. Ryder <_mwryder@worldnet.att.net> 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.

I'm guessing this behavior is due to readline, which is normally
compiled into into irb if available. If so, you can also insert tabs
using M-TAB (a.k.a. alt-tab). Try "info readline" for more info (or
google "info readline" if you don't have it available).

Michael W. Ryder

1/23/2007 2:06:00 AM

0

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"
>

Thank you for the tip. It is going to be hard to remember which string
representation to use for what purpose but I guess that is part of
learning the language.

Michael W. Ryder

1/23/2007 2:14:00 AM

0

George Ogata wrote:
> On 1/23/07, Michael W. Ryder <_mwryder@worldnet.att.net> 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.
>
> I'm guessing this behavior is due to readline, which is normally
> compiled into into irb if available. If so, you can also insert tabs
> using M-TAB (a.k.a. alt-tab). Try "info readline" for more info (or
> google "info readline" if you don't have it available).
>

Using alt-tab on my XP Pro computer results in another window popping up
and does not insert a tab character into the string. I will just have
to remember to use double quotes and \t for tabs.