Unexpected behavior while doing interpolation inside %W() construct

Hi,

Look the below code :-

#!/usr/bin/env ruby

expected

%W(“hello world”) # => ["“hello”, “world”"]

#unexpected
str = “hello world”
%W(#{str}) # => [“hello world”]

Can anyone explain these 2 different behavior ?

Regards,
Arup R.

Debugging is twice as hard as writing the code in the first place.
Therefore,
if you write the code as cleverly as possible, you are, by definition,
not
smart enough to debug it.

–Brian Kernighan

You would have to do %W(#{str1} #{str2})

See:

landau:internal dsferreira$ irb
1.9.3-p392 :001 > a = ‘"a’
=> ““a”
1.9.3-p392 :002 > b = 'b”’
=> “b”"
1.9.3-p392 :003 > %W(#{a} #{b})
=> ["“a”, “b”"]
1.9.3-p392 :004 >

cheers,

Daniel

On Saturday, July 05, 2014 02:29:18 PM Daniel da Silva Ferreira wrote:

=> ["“a”, “b”"]
1.9.3-p392 :004 >

cheers,

Daniel

That’s not my question. :slight_smile:

Regards,
Arup R.

Debugging is twice as hard as writing the code in the first place.
Therefore,
if you write the code as cleverly as possible, you are, by definition,
not
smart enough to debug it.

–Brian Kernighan

If you use %W() the character " is not special inside it. Look at it
again
knowing that " is not special.
Em 05/07/2014 10:26, “Arup R.” [email protected]
escreveu:

#unexpected
str = “hello world”
%W(#{str}) # => [“hello world”]

I believe the easiest way to understand this is that the %W does not do
the
string interpolation until AFTER it has broken down the elements of the
input using space as the delimiter so the %W call never sees your space
inside the string.

John

Hi John,

I would say so but to be completely sure I would have to look at how it
is being implemented.
Anyone knows the file where this methods are defined in ruby core?

Thanks,

Daniel

On Sat, Jul 5, 2014 at 8:20 PM, Daniel da Silva Ferreira
[email protected] wrote:

Hi John,

I would say so but to be completely sure I would have to look at how it is
being implemented.
Anyone knows the file where this methods are defined in ruby core?

I think this is the place where this is defined, in parse.y:

https://github.com/ruby/ruby/blob/trunk/parse.y#L3969

words is:

| tWORDS_BEG word_list tSTRING_END

where tWORDS_BEG is %W (you can find that searching in that file), and
word_list is (line 3979):

| word_list word ’ ’

So, it’s the parser who divides tokens between spaces when it finds
%W, passing then each token to

list_append($1, evstr2dstr($2));

So this means that the evaluation of the interpolation results in just
one element in the array, no matter if it contains spaces or not.

Jesus.

Is there any place where you can find documentation about this internal
things?

Good question.

Regards,
Arup R.

Debugging is twice as hard as writing the code in the first place.
Therefore,
if you write the code as cleverly as possible, you are, by definition,
not
smart enough to debug it.

–Brian Kernighan

Thanks Jesus.

No way I would be able to get there.
I tried to search for %W.

In the end everything is handled by the parser.

Is there any place where you can find documentation about this internal
things?

Regards,

Daniel

On Sat, Jul 5, 2014 at 12:10 PM, Daniel da Silva Ferreira <
[email protected]> wrote:

Thanks Jesus.

No way I would be able to get there.
I tried to search for %W.

In the end everything is handled by the parser.

Is there any place where you can find documentation about this internal
things?

For this depth of information, the code will always be the best place to
look.