Placing tabs in strings in irb

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.

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

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

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

MikeGee 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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

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

Lincoln Anderson wrote:

character or places a random value in the string.

=>“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

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

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

Nathan S. wrote:

In irb if you do a=“1\t2\t3\t4” it replies with “1\t2\t3\t4”. But, if

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.mozdev.org

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

Joel VanderWerf wrote:

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.

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

On 1/23/07, Michael W. Ryder [email protected] 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).

MikeGee wrote:

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.

George O. wrote:

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.

On 1/23/07, Michael W. Ryder [email protected] wrote:

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.

Ah, yes, readline is more of a unixy thing AFAIK. Sorry, I’m not sure
about Windows.

On Jan 22, 2007, at 9:31 PM, George O. wrote:

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.

Ah, yes, readline is more of a unixy thing AFAIK. Sorry, I’m not sure
about Windows.

But M-TAB is just shorthand for ESC TAB (hit ESC then hit TAB). It
works for Mac, I’m guessing that it works the same for Windows.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Rob B. wrote:

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.

Ah, yes, readline is more of a unixy thing AFAIK. Sorry, I’m not sure
about Windows.

But M-TAB is just shorthand for ESC TAB (hit ESC then hit TAB). It
works for Mac, I’m guessing that it works the same for Windows.

That works but is harder to enter then \t so I will stay with the \t
convention. Thank you for the tip on M-TAB being ESC-TAB.

Michael W. Ryder:

Is there any reason I cannot embed tabs in a string while using irb?

Ctrl-V, TAB is working for me. I don’t know what environment traits
this
is specific to, though.

Kalman