# Split question

**URL:** <https://www.ruby-forum.com/t/split-question/176907>\
**Category:** Ruby\
**Created:** [October 7, 2009, 11:29pm UTC](https://www.ruby-forum.com/t/split-question/176907 "2009-10-07T23:29:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 7, 2009, 11:29pm UTC](https://www.ruby-forum.com/t/split-question/176907/1 "2009-10-07T23:29:13Z")

</div>

Hi all,

I try to split a line that is separated by ‘tab’ and expect return an  
array with all the elements containing empty space. But my code doesn’t  
work. Any idea?

Thanks,

Li

C:\Documents and Settings\chen41\Desktop\>irb  
irb(main):001:0\> s=’\t\t\t\t\t\t\t\t’  
=\> “\t\t\t\t\t\t\t\t”  
irb(main):002:0\> s.split(/\t/)  
=\> ["\t\t\t\t\t\t\t\t"]  
irb(main):003:0\> s.split(/\t/)  
=\> []  
irb(main):004:0\>

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 7, 2009, 11:46pm UTC](https://www.ruby-forum.com/t/split-question/176907/2 "2009-10-07T23:46:41Z")

</div>

On Oct 7, 2009, at 14:29 , Li Chen wrote:

> C:\Documents and Settings\chen41\Desktop\>irb  
> irb(main):001:0\> s=’\t\t\t\t\t\t\t\t’  
> =\> “\t\t\t\t\t\t\t\t”

These aren’t tabs. They’re “” followed by “t”, you can tell because  
the “” is backslashed, meaning it is a literal backslash.

Use double quotes instead.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 7, 2009, 11:48pm UTC](https://www.ruby-forum.com/t/split-question/176907/3 "2009-10-07T23:48:26Z")

</div>

On Wed, Oct 7, 2009 at 4:29 PM, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> C:\Documents and Settings\chen41\Desktop\>irb

Single quotes treat things literally, like you can’t interpolate, and \t  
is  
treated as two separate characters.

$irb

> > RUBY\_VERSION  
> > =\> “1.8.6”

> > s = ‘\t\t\t\t\t\t\t\t’  
> > =\> “\t\t\t\t\t\t\t\t”

> > s = “\t\t\t\t\t\t\t\t”  
> > =\> “\t\t\t\t\t\t\t\t”

> > s.split /\t/  
> > =\> []

> > ‘abc#{2}def’  
> > =\> “abc#{2}def”

> > “abc#{2}def”  
> > =\> “abc2def”

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 7, 2009, 11:57pm UTC](https://www.ruby-forum.com/t/split-question/176907/4 "2009-10-07T23:57:31Z")

</div>

Ryan D. wrote:

> On Oct 7, 2009, at 14:29 , Li Chen wrote:
> 
> > C:\Documents and Settings\chen41\Desktop\>irb  
> > irb(main):001:0\> s=’\t\t\t\t\t\t\t\t’  
> > =\> “\t\t\t\t\t\t\t\t”
> 
> These aren’t tabs. They’re “” followed by “t”, you can tell because  
> the “” is backslashed, meaning it is a literal backslash.
> 
> Use double quotes instead.

Here is what I get using double quotes. I get an empty array only.

C:\Documents and Settings\chen41\Desktop\>irb  
irb(main):001:0\> s="\t\t\t\t\t\t\t\t"  
=\> “\t\t\t\t\t\t\t\t”  
irb(main):002:0\> s.split(/\t/)  
=\> []  
irb(main):003:0\>

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 7, 2009, 11:59pm UTC](https://www.ruby-forum.com/t/split-question/176907/5 "2009-10-07T23:59:30Z")

</div>

Li Chen wrote:

> Ryan D. wrote:
> 
> > On Oct 7, 2009, at 14:29 , Li Chen wrote:
> > 
> > > C:\Documents and Settings\chen41\Desktop\>irb  
> > > irb(main):001:0\> s=’\t\t\t\t\t\t\t\t’  
> > > =\> “\t\t\t\t\t\t\t\t”
> > 
> > These aren’t tabs. They’re “” followed by “t”, you can tell because  
> > the “” is backslashed, meaning it is a literal backslash.
> > 
> > Use double quotes instead.
> 
> Here is what I get using double quotes. I get an empty array only.
> 
> C:\Documents and Settings\chen41\Desktop\>irb  
> irb(main):001:0\> s="\t\t\t\t\t\t\t\t"  
> =\> “\t\t\t\t\t\t\t\t”  
> irb(main):002:0\> s.split(/\t/)  
> =\> []  
> irb(main):003:0\>

str.split takes two parameters: the pattern and optionally the “limit”  
From the docs:  
If the limit parameter is omitted, trailing null fields are suppressed.  
If limit is a positive number, at most that number of fields will be  
returned (if limit is 1, the entire string is returned as the only entry  
in an array). If negative, there is no limit to the number of fields  
returned, and trailing null fields are not suppressed.

So

s = “\t\t\t\t”  
s.split("\t") #all null fields  
#=\> []  
s.split("\t",-1)  
#=\> ["", “”, “”, “”]

hth,

Siep

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 8, 2009, 12:03am UTC](https://www.ruby-forum.com/t/split-question/176907/6 "2009-10-08T00:03:04Z")

</div>

Hi –

On Thu, 8 Oct 2009, Siep K. wrote:

> > > the "" is backslashed, meaning it is a literal backslash.  
> > > =\> []  
> > > So
> 
> s = “\t\t\t\t”  
> s.split(“\t”) #all null fields  
> #=\> []  
> s.split(“\t”,-1)  
> #=\> [“”, “”, “”, “”]

[“”, “”, “”, “”, “”] I think 🙂

David

–  
The Ruby training with D. Black, G. Brown, J.McAnally  
Compleat Jan 22-23, 2010, Tampa, FL  
Rubyist [http://www.thecompleatrubyist.com](http://www.thecompleatrubyist.com)

David A. Black/Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
