[Rspec-devel] define_instance_method, stub_with, and mock_with

Brian Takita brian.takita at gmail.com
Mon Aug 28 00:04:29 EDT 2006


>
> Why not just do:
>
> o = Object.new
> def o.foo
>  :bar
> end
> o.foo # return :bar
>
> Is it just to get the raise when undefined methods are being (re)defined?
>

Because you lose scope.

o = Object.new
return_value = :bar

def o.foo
  return_value
end

o.foo # returns nil

But

o = Object.new
return_value = :bar

o.define_instance_method(:foo) {return_value}
o.foo # returns :bar

Its also useful for checking what parameters where passed in.

o = Object.new
passed_arg = nil
o.define_instance_method(:foo) {|arg| passed_arg = arg}
o.foo(:bar)
passed_arg == :bar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-devel/attachments/20060827/a51550b0/attachment.html 


More information about the Rspec-devel mailing list