[rspec-users] Noob syntax questions regarding rspec book...

David Chelimsky dchelimsky at gmail.com
Tue Jul 7 12:07:07 EDT 2009

On Tue, Jul 7, 2009 at 10:35 AM, Chris
Sund<chris at silhouettesolutions.net> wrote:
> Hey Everyone,
> I've been working my way through the Rspec book trying to absorb and
> understand everything. This is my first time with BDD and I'm just
> trying to figure out some simple syntax stuff. My questions revolve
> around some of the syntaxing used in the book. These are really simple
> questions.

Hey Chris,

The book assumes a basic working knowledge of Ruby. The questions you
are asking are about Ruby, not about RSpec or Cucumber. I'll answer
them for you here, but I don't think this is appropriate or necessary
for the book itself.

> 1.) Given /^the secret code is (. . . .)$/ do |code|
>        Is (. . . .) simply a place holder? could I use something like
> (- - - -) instead, or does it actually mean something?

The argument to Given is a regular expression. The "." means "any
character", so (. . . .) means any four characters with spaces in
between. There are certainly more specific and fool-proof ways we
could express this, but this is a very simple way, and works just fine
in the context of a cucumber step definition.

> 2.) Then /^the mark should be (.*)$/ do |mark|
>        Similar question....what does .* represent?

The "." means any character and the "*" means any number of times.

> 3.) In the following example why don't I pass   |guess| to the When
> statement? I'm sure it has something to do with the (code.split)
> syntax, I'm just not sure what.
> When /^I guess (. . . .)$/ do |code|
> @game.guess(code.split)
> end

You're correct - code.split converts "r y g c" to ['r', 'y', 'g',
'c'], which is what the game wants to receive (an array, rather than a

> 4.) And finally what does ("\n") do?
> Then /^the mark should be (.*)$/ do |mark|
>  @messenger.string.split("\n").should include(mark)
> end

The @messenger is a StringIO object. It receives puts() statements and
adds them to it's string attribute with a line break ("\n") at the end
so if you do this:

@messenger.puts "a"
@messenger.puts "b"

Then the result of @messenger.string is "a\nb\n"

Splitting that on "\n" results in ["a","b"], which allows you to ask
if that result includes "a", for example:


Which can be expressed as an expectation in RSpec like this:

  @messenger.split("\n").should include("a")


> Thank You!
> Chris
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list