Cucumber - but really ruby

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.

w% is a ruby notation. It creates an Array of of the literal string
you type separated by white space

Emmanuel

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. wrote:

Thank you. What does %[…] do?

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

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.

On Tue, Feb 3, 2009 at 12:49 PM, James B. [email protected]
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

%w == string.split(/\s+/) (maybe not exactly
that regexp, but basically it’s an array of the words–hence the w)

% == 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

Could not remember that one but based on testing on irb, seems to do
the reverse :slight_smile:

Array to string.

so %[test blah] becomes “test blah”

A full listing of this stuff can be found here:

% appears to be the same thing as %Q

On Tue, Feb 3, 2009 at 1:10 PM, James B. [email protected]
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

David C. wrote:

On Tue, Feb 3, 2009 at 1:10 PM, James B. [email protected]
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.

Explanation:
http://jimhoskins.com/2008/10/07/ruby-percent-syntax-percent-functions/

Ngoc.

Uh, seems my email never arrived? This is what I had written:

%w == string.split(/\s+/) (maybe not exactly
that regexp, but basically it’s an array of the words–hence the w)

% == 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