Cucumber - but really ruby


#1

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.


#2

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

Emmanuel


#3

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?


#4

James B. wrote:

Thank you. What does %[…] do?

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


#5

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.


#6

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


#7

%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


#8

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”


#9

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


#10

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


#11

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.


#12

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

Ngoc.


#13

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