On Wed, Mar 5, 2008 at 12:06 PM, Corey Haines <[EMAIL PROTECTED]> wrote:
>
> On Wed, Mar 5, 2008 at 2:17 PM, Pat Maddox <[EMAIL PROTECTED]> wrote:
> >
> >
> > You need to splat em out:
> >
> >
> > and_return(*existing_quote_numbers.concat([nil]))
> >
> > Pat
> >
>
> Hi, Pat,
>
> Just for my own edification and education, if you have a minute, could you
> explain what this means and why you have to do it?

Basically it takes an array and splits it up.  Here's a little irb
session that ought to demonstrate it.

>> def foo(val); p val; end
=> nil
>> a = [1,2,3]
=> [1, 2, 3]
>> foo(a)
[1, 2, 3]
=> nil
>> foo(*a)
ArgumentError: wrong number of arguments (3 for 1)
        from (irb):4:in `foo'
        from (irb):4

The method expects one argument.  When I pass it an array, everything
is fine because it only has one object.  However when I splat the
array, it splits it up into three distinct objects and tries to pass
it in, causing the method to blow up.

In RSpec, you specify multiple values to return by passing in multiple
objects.  An array is just one object, so the mock just returns it.
Splat it out and you've passed multiple values to and_return, causing
it to return them in succession.

* is called the splat operator and can be used to collect stuff in
addition to splitting stuff up.  Just google for "ruby splat operator"
to get lots of info.  Also you can define #to_splat on any object.

Pat
_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to