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

Ben Mabey ben at benmabey.com
Tue Jul 7 13:11:51 EDT 2009


Rick DeNatale wrote:
> On Tue, Jul 7, 2009 at 11: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.
>>
>>
>> 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?
>>     
>
> Yes it actually means something. the stuff between // is a regular
> expression, and some of the characters have meaning.
>
>    ^ means the beginning of the string
>    $ means the end of the string
>    Each . will mark a single character, any character will do.
>   The parens mark a group, the part of the string which marks the
> group will be assigned to the code parameter.
>   So when the whole regex matches code will be set to the four
> characters of the code separated by spaces.
>   
>> 2.) Then /^the mark should be (.*)$/ do |mark|
>>        Similar question....what does .* represent?
>>     
>
> it means zero or more arbitrary characters
>
>   
>> 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
>>     
>
> There isn't a variable named guess here. As I said in answer to the
> first question, if the story says
>
>     When I guess 1 3 4 2
>
> then when the step is executed the code parameter to the block will be
> set to "1 3 4 2" and "1 3 4 2".split gives ["1", "3", "4", "2"]
>   
>> 4.) And finally what does ("\n") do?
>>
>> Then /^the mark should be (.*)$/ do |mark|
>>  @messenger.string.split("\n").should include(mark)
>> end
>>     
>
> "\n" is a ruby string literal representing a new-line, so
>    @messenger.string.split("\n") results in an array comprising each
> line within @messenger.string
>
>
>
>   


Chris,
FYI, a good resource to learn and play around with reg exps in ruby is: 
http://rubular.com/

-Ben


More information about the rspec-users mailing list