Placing tabs in strings in irb

M

Michael W. Ryder

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

MikeGee

Michael said:
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"
 
L

Lincoln Anderson

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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.mozdev.org

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

Wolfgang Nádasi-Donner

Lincoln said:
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
 
T

Timothy Hunter

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


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
 
J

Joel VanderWerf

Michael said:
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
 
L

Lincoln Anderson

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

Nathan said:
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.


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

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

Michael W. Ryder

Joel said:
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.
 
G

George Ogata

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

Michael W. Ryder

MikeGee said:
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.
 
M

Michael W. Ryder

George said:
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.
 
G

George Ogata

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

Kalman Noel

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
 
M

Michael W. Ryder

Rob said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,222
Messages
2,571,142
Members
47,757
Latest member
PDIJaclyn

Latest Threads

Top