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.
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-02-03 19: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.
77dae8923911b83d3d4e1fbb22d95cd8?d=identicon&s=25 Emmanuel Pinault (Guest)
on 2009-02-03 20: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
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-02-03 20:04
Emmanuel Pinault 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?
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-02-03 20:10
James Byrne wrote:
>
> Thank you. What does %[...] do?

This would not happen to be the same thing as %Q[..] would it?
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-02-03 20:18
James Byrne 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.
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-02-03 20:23
(Received via mailing list)
On Tue, Feb 3, 2009 at 12:49 PM, James Byrne <lists@ruby-forum.com>
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
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-02-03 20:24
(Received via mailing list)
On Tue, Feb 3, 2009 at 1:10 PM, James Byrne <lists@ruby-forum.com>
wrote:
> James Byrne 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
De03af350d099d28a0dd223ed5ffdd37?d=identicon&s=25 Nicolás Sanguinetti (Guest)
on 2009-02-03 20: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
77dae8923911b83d3d4e1fbb22d95cd8?d=identicon&s=25 Emmanuel Pinault (Guest)
on 2009-02-03 20: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"
Ff937b6907db49432c980f2b6a5c7e71?d=identicon&s=25 Mischa Fierer (mischa)
on 2009-02-03 20: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
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-02-03 20:52
David Chelimsky wrote:
> On Tue, Feb 3, 2009 at 1:10 PM, James Byrne <lists@ruby-forum.com>
> wrote:
>> James Byrne 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.
De03af350d099d28a0dd223ed5ffdd37?d=identicon&s=25 Nicolás Sanguinetti (Guest)
on 2009-02-03 21: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
1fe51bee1126a6f9234e0a5e5078bbcf?d=identicon&s=25 Ngoc Dao (Guest)
on 2009-02-03 23:01
(Received via mailing list)
This topic is locked and can not be replied to.