# Placing tabs in strings in irb

**URL:** <https://rubytalk.org/t/placing-tabs-in-strings-in-irb/34711>\
**Category:** ruby-talk\
**Created:** [23 January 2007 00:45 UTC](https://rubytalk.org/t/placing-tabs-in-strings-in-irb/34711 "2007-01-23T00:45:06Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_W\_Ryder1](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Michael\_W\_Ryder1](https://rubytalk.org/u/Michael_W_Ryder1)\
**Post date:** [23 January 2007 00:45 UTC](https://rubytalk.org/t/placing-tabs-in-strings-in-irb/34711/1 "2007-01-23T00:45:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MikeGee](https://avatars.discourse-cdn.com/v4/letter/m/ccd318/32.png) [@MikeGee](https://rubytalk.org/u/MikeGee)\
**Post date:** [23 January 2007 00:50 UTC](https://rubytalk.org/t/placing-tabs-in-strings-in-irb/34711/2 "2007-01-23T00:50:09Z")

</div>

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"

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [23 January 2007 01:25 UTC](https://rubytalk.org/t/placing-tabs-in-strings-in-irb/34711/3 "2007-01-23T01:25:04Z")

</div>

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"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):4:in `Integer'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):4

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![George3](https://avatars.discourse-cdn.com/v4/letter/g/51bf81/32.png) [@George3](https://rubytalk.org/u/George3)\
**Post date:** [23 January 2007 01:58 UTC](https://rubytalk.org/t/placing-tabs-in-strings-in-irb/34711/4 "2007-01-23T01:58:06Z")

</div>

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

> **···**
>
> 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.

---

<div class="post-metadata">

**Author:** ![Kalman\_Noel](https://avatars.discourse-cdn.com/v4/letter/k/ad7895/32.png) [@Kalman\_Noel](https://rubytalk.org/u/Kalman_Noel)\
**Post date:** [23 January 2007 19:45 UTC](https://rubytalk.org/t/placing-tabs-in-strings-in-irb/34711/5 "2007-01-23T19:45:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Lincoln\_Anderson](https://avatars.discourse-cdn.com/v4/letter/l/e79b87/32.png) [@Lincoln\_Anderson](https://rubytalk.org/u/Lincoln_Anderson)\
**Post date:** [23 January 2007 01:06 UTC](https://rubytalk.org/t/placing-tabs-in-strings-in-irb/34711/6 "2007-01-23T01:06:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_W\_Ryder1](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Michael\_W\_Ryder1](https://rubytalk.org/u/Michael_W_Ryder1)\
**Post date:** [23 January 2007 01:55 UTC](https://rubytalk.org/t/placing-tabs-in-strings-in-irb/34711/7 "2007-01-23T01:55:08Z")

</div>

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"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):4:in `Integer'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![Michael\_W\_Ryder1](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Michael\_W\_Ryder1](https://rubytalk.org/u/Michael_W_Ryder1)\
**Post date:** [23 January 2007 02:05 UTC](https://rubytalk.org/t/placing-tabs-in-strings-in-irb/34711/8 "2007-01-23T02:05:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_W\_Ryder1](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Michael\_W\_Ryder1](https://rubytalk.org/u/Michael_W_Ryder1)\
**Post date:** [23 January 2007 02:15 UTC](https://rubytalk.org/t/placing-tabs-in-strings-in-irb/34711/9 "2007-01-23T02:15:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Nathan\_Smith](https://avatars.discourse-cdn.com/v4/letter/n/839c29/32.png) [@Nathan\_Smith](https://rubytalk.org/u/Nathan_Smith)\
**Post date:** [23 January 2007 01:17 UTC](https://rubytalk.org/t/placing-tabs-in-strings-in-irb/34711/10 "2007-01-23T01:17:47Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![Wolfgang\_Nadasi-Don1](https://avatars.discourse-cdn.com/v4/letter/w/958977/32.png) [@Wolfgang\_Nadasi-Don1](https://rubytalk.org/u/Wolfgang_Nadasi-Don1)\
**Post date:** [23 January 2007 01:20 UTC](https://rubytalk.org/t/placing-tabs-in-strings-in-irb/34711/11 "2007-01-23T01:20:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tim\_Hunter4](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@Tim\_Hunter4](https://rubytalk.org/u/Tim_Hunter4)\
**Post date:** [23 January 2007 01:22 UTC](https://rubytalk.org/t/placing-tabs-in-strings-in-irb/34711/12 "2007-01-23T01:22:28Z")

</div>

Lincoln Anderson wrote:

> -----BEGIN PGP SIGNED MESSAGE-----  
> Hash: SHA1
> 
> MikeGee wrote:  
> &nbsp;&nbsp;
> 
> > Michael W. Ryder wrote:  
> > &nbsp;&nbsp;&nbsp;&nbsp;
> > 
> > > 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.  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
> > 
> > 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).  
> &nbsp;&nbsp;  
> 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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str.to\_i(base=10) =\> integer

> **···**
>
> ------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the result of interpreting leading characters in str as an  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;integer base base (2, 8, 10, or 16). Extraneous characters past  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the end of a valid number are ignored. If there is not a valid  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;number at the start of str, 0 is returned. This method never  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raises an exception.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"12345".to\_i #=\> 12345  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"99 red balloons".to\_i #=\> 99

---

<div class="post-metadata">

**Author:** ![George3](https://avatars.discourse-cdn.com/v4/letter/g/51bf81/32.png) [@George3](https://rubytalk.org/u/George3)\
**Post date:** [23 January 2007 02:31 UTC](https://rubytalk.org/t/placing-tabs-in-strings-in-irb/34711/13 "2007-01-23T02:31:24Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Lincoln\_Anderson](https://avatars.discourse-cdn.com/v4/letter/l/e79b87/32.png) [@Lincoln\_Anderson](https://rubytalk.org/u/Lincoln_Anderson)\
**Post date:** [23 January 2007 01:26 UTC](https://rubytalk.org/t/placing-tabs-in-strings-in-irb/34711/14 "2007-01-23T01:26:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [23 January 2007 14:33 UTC](https://rubytalk.org/t/placing-tabs-in-strings-in-irb/34711/15 "2007-01-23T14:33:38Z")

</div>

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 Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com

> **···**
>
> On Jan 22, 2007, at 9:31 PM, George Ogata wrote:
> 
> > On 1/23/07, Michael W. Ryder \<\_mwryder@worldnet.att.net\> 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.

---

<div class="post-metadata">

**Author:** ![Michael\_W\_Ryder1](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Michael\_W\_Ryder1](https://rubytalk.org/u/Michael_W_Ryder1)\
**Post date:** [23 January 2007 19:50 UTC](https://rubytalk.org/t/placing-tabs-in-strings-in-irb/34711/16 "2007-01-23T19:50:10Z")

</div>

Rob Biedenharn 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.
> 
> 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.

> **···**
>
> > On Jan 22, 2007, at 9:31 PM, George Ogata wrote:
> > 
> > > On 1/23/07, Michael W. Ryder \<\_mwryder@worldnet.att.net\> wrote:
> 
> > -Rob
> > 
> > Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> > Rob@AgileConsultingLLC.com
