Doubts on char patterns into String.each argument

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.

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?

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

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

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 :frowning:
it might be better if you cutnpaste actual code run.

kind regards -botp