# Doubts on char patterns into String.each argument

**URL:** <https://www.ruby-forum.com/t/doubts-on-char-patterns-into-string-each-argument/111324>\
**Category:** Ruby\
**Created:** [August 6, 2007, 1:43am UTC](https://www.ruby-forum.com/t/doubts-on-char-patterns-into-string-each-argument/111324 "2007-08-06T01:43:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [August 6, 2007, 1:43am UTC](https://www.ruby-forum.com/t/doubts-on-char-patterns-into-string-each-argument/111324/1 "2007-08-06T01:43:09Z")

</div>

Hello Folks with a question I would like someone can clarify me:

When I type the following code:

“Substring1 Substring2 Substring3”.each( ‘\s\*’ ) { | substring |  
puts “[” + substring +"]"  
}

What I got is:

[Substring1]  
[Substring2]  
[Substring3]

However when I type:

“Substring1 Substring2 Substring3”.each( ‘\s\*’ ) { | substring |  
puts “[” + substring +"]"  
}

What I got is:

[Substring1 Substring2 Substring3]

Why the difference in outputs?  
I verified in the “Programming Ruby 2nd (Dave T. - 2005)”  
and it says that the ’ ’ is just one of the White Characters  
that ‘\s’ may represent ( the other ones are TAB and NEW LINE)

Any help would be very appreciated.

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [August 6, 2007, 2:50am UTC](https://www.ruby-forum.com/t/doubts-on-char-patterns-into-string-each-argument/111324/2 "2007-08-06T02:50:59Z")

</div>

Hi,

At Mon, 6 Aug 2007 08:43:07 +0900,  
Carlos O. wrote in [ruby-talk:263437]:

> When I type the following code:
> 
> “Substring1 Substring2 Substring3”.each( ‘\s\*’ ) { | substring |  
> puts “[” + substring +"]"  
> }

What differs?

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [August 6, 2007, 3:11am UTC](https://www.ruby-forum.com/t/doubts-on-char-patterns-into-string-each-argument/111324/3 "2007-08-06T03:11:00Z")

</div>

On Mon, 06 Aug 2007 08:43:07 +0900, Carlos O. wrote:

> “Substring1 Substring2 Substring3”.each( ‘\s\*’ ) { | substring |  
> puts “[” + substring +"]"  
> }

In ruby, if you want something to be interpreted as a regular  
expression,  
you must declare it as a regular expression. Hence /\s\*/ would be  
correct. Except that each() only accepts strings, so you can’t use a  
regular expression with it.

Use  
“Substring1 Substring2 Substring3”.split(/\s+/).each{|x| puts x}  
instead

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [August 6, 2007, 4:01am UTC](https://www.ruby-forum.com/t/doubts-on-char-patterns-into-string-each-argument/111324/4 "2007-08-06T04:01:07Z")

</div>

Hi –

On Mon, 6 Aug 2007, Ken B. wrote:

> Use  
> “Substring1 Substring2 Substring3”.split(/\s+/).each{|x| puts x}  
> instead

You could also just do:

puts “Substring1 Substring2 Substring3”.split(’ ')

You could even leave off the (’ '), except then if $; has a value  
you’d end up splitting on that.

David

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [August 6, 2007, 3:58am UTC](https://www.ruby-forum.com/t/doubts-on-char-patterns-into-string-each-argument/111324/5 "2007-08-06T03:58:07Z")

</div>

On Behalf Of Carlos O.:

# and it says that the ’ ’ is just one of the White Characters

# that ‘\s’ may represent ( the other ones are TAB and NEW LINE)

careful with “” vs ‘’

irb(main):020:0\> puts “\s”

=\> nil  
irb(main):021:0\> puts ‘\s’  
\s  
=\> nil  
irb(main):022:0\>  
irb(main):023:0\* p “\s”  
" "  
=\> nil  
irb(main):024:0\> p ‘\s’  
“\s”  
=\> nil

careful with “\s” vs /\s/ vs ‘\s’

irb(main):013:0\> " \t\n\*\n".split("\s\*")  
=\> [" \t\n\*\n"]  
irb(main):014:0\> " \t\n _\n".split("\s_")  
=\> [" \t\n", “\n”]  
irb(main):015:0\> " \t\n _\n".split("\s")  
=\> ["_"]  
irb(main):016:0\> " \t\n _\n".split(/\s_/)  
=\> ["", “\*”]  
irb(main):017:0\> " \t\n _\n".split(/\s/)  
=\> ["", “”, “”, “”, "_"]  
irb(main):018:0\> " \t\n _\n".split(/\s+/)  
=\> ["", "_"]  
irb(main):019:0\> " \t\n _\n".split(’\s_’)  
=\> [" \t\n \*\n"]  
irb(main):020:0\> " \t\n \*\n".split(’\s’)  
=\> [" \t\n \*\n"]

irb(main):043:0\> “Substring1\t\n Substring2\tSubstring3”.each( “\s” ) {  
| substring |  
irb(main):044:1\* puts “[” + substring +"]"  
irb(main):045:1\> }  
[Substring1  
]  
[Substring2 Substring3]

read on string.each, string.split, string.scan

try,  
$\> qri string.each string.split string.scan

btw, your 2 sample codes do not differ but on your believed output ☹  
it might be better if you cutnpaste actual code run.

kind regards -botp
