Forum: RSpec Cucumber - but really ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
James B. (Guest)
on 2009-02-03 20:49
In the cucumber rake task I see this construct:

    t.rcov_opts = %w{--rails --exclude
lib\/ruby,lib64\/ruby,\/usr,osx\/objc,gems\/,test\/,spec\/,features\/}
    t.rcov_opts << %[--output "coverage"]

My questions is:  What do %w and % represent with respect to the
"block"? in the first case and the "array"? in the second?  I cannot
find an explanation of either usage in the documentation.
Emmanuel P. (Guest)
on 2009-02-03 21:00
(Received via mailing list)
w% is a ruby notation. It creates an Array of of the literal string
you type separated by white space

Emmanuel
James B. (Guest)
on 2009-02-03 21:04
Emmanuel P. wrote:
> w% is a ruby notation. It creates an Array of of the literal string
> you type separated by white space
>
> Emmanuel

Thank you. What does %[...] do?
James B. (Guest)
on 2009-02-03 21:10
James B. wrote:
>
> Thank you. What does %[...] do?

This would not happen to be the same thing as %Q[..] would it?
James B. (Guest)
on 2009-02-03 21:18
James B. wrote:

>
> This would not happen to be the same thing as %Q[..] would it?

Apparently, the construct:

    t.rcov_opts = %w{--rails --exclude
lib\/ruby,lib64\/ruby,\/usr,osx\/objc,gems\/,test\/,spec\/,features\/}

    t.rcov_opts << %[--output "coverage"]

is equivalent to:

    t.rcov_opts = [ "--rails"
      "--exclude"
      "lib\/ruby,lib64\/ruby,\/usr,osx\/objc,gems\/,test\/,spec\/,features\/"
      "--output" "coverage"
                  ]
Posted so that I can google for it next time.
David C. (Guest)
on 2009-02-03 21:23
(Received via mailing list)
On Tue, Feb 3, 2009 at 12:49 PM, James B. <removed_email_address@domain.invalid>
wrote:
> In the cucumber rake task I see this construct:
>
>    t.rcov_opts = %w{--rails --exclude
> lib\/ruby,lib64\/ruby,\/usr,osx\/objc,gems\/,test\/,spec\/,features\/}
>    t.rcov_opts << %[--output "coverage"]
>
> My questions is:  What do %w and % represent with respect to the
> "block"? in the first case and the "array"? in the second?  I cannot
> find an explanation of either usage in the documentation.

They're not really a block and an array - the %w can be followed by
the delimiter of your choice, and makes an array of the words between
the first and next instance of that delimiter. For example, each of
these:

%w{these words}
%w[these words]
%w|these words|
%w%these words%

... all produce this array:

["these","words"]

HTH,
David
David C. (Guest)
on 2009-02-03 21:24
(Received via mailing list)
On Tue, Feb 3, 2009 at 1:10 PM, James B. <removed_email_address@domain.invalid>
wrote:
> James B. wrote:
>>
>> Thank you. What does %[...] do?
>
> This would not happen to be the same thing as %Q[..] would it?

Nope.

http://www.rubycentral.com/pickaxe/tut_stdtypes.html

scroll down to strings
Nicolás Sanguinetti (Guest)
on 2009-02-03 21:35
(Received via mailing list)
%w<delim><string><delim> == string.split(/\s+/) (maybe not exactly
that regexp, but basically it's an array of the words--hence the w)

%<delim><string><delim> == string, but without needing to escape the
quotes. It's the same as using %Q (ie, it understands escape sequences
and interpolated expressions inside, while %q is a literal string)

HTH
-foca
Emmanuel P. (Guest)
on 2009-02-03 21:44
(Received via mailing list)
Could not remember that one but based on testing on irb, seems to do
the reverse :)

Array to string.

so %[test blah]  becomes "test blah"
Mischa F. (Guest)
on 2009-02-03 21:46
(Received via mailing list)
A full listing of this stuff can be found here:

http://www.zenspider.com/Languages/Ruby/QuickRef.html#6

% appears to be the same thing as %Q
James B. (Guest)
on 2009-02-03 21:52
David C. wrote:
> On Tue, Feb 3, 2009 at 1:10 PM, James B. <removed_email_address@domain.invalid>
> wrote:
>> James B. wrote:
>>>
>>> Thank you. What does %[...] do?
>>
>> This would not happen to be the same thing as %Q[..] would it?
>
> Nope.
>
> http://www.rubycentral.com/pickaxe/tut_stdtypes.html
>
> scroll down to strings

I did. I can find nothing that discusses the %[] construct.
Nicolás Sanguinetti (Guest)
on 2009-02-03 22:48
(Received via mailing list)
Uh, seems my email never arrived? This is what I had written:

%w<delim><string><delim> == string.split(/\s+/) (maybe not exactly
that regexp, but basically it's an array of the words--hence the w)

%<delim><string><delim> == string, but without needing to escape the
quotes. It's the same as using %Q (ie, it understands escape sequences
and interpolated expressions inside, while %q is a literal string)

HTH
-foca
Ngoc Dao (Guest)
on 2009-02-04 00:01
(Received via mailing list)
This topic is locked and can not be replied to.