[rspec-users] and_yield + instance_eval(&block)

David Chelimsky dchelimsky at gmail.com
Fri Aug 10 15:43:54 EDT 2007


On 8/7/07, Scott Taylor <mailing_lists at railsnewbie.com> wrote:
>
> I have the following code, which yields instance eval's the block given:
>
> class Foo
>
>    def bar(&blk)
>      instance_eval &blk
>    end
>
>    def baz
>      yield
>    end
>
> end
>
> The effect of this is that self is reassigned:
>
> Foo.new.bar do
>     # here, self is the instance of Foo
>     # created by new
> end

Why not just do this, which is already a language construct?

Foo.new.instance_eval { ... }

>
> But normally self is the object in which
> Foo.new.bar {...} occurs.
>
> Foo.new.baz do
>
>    # self is the execution context
>    # in which Foo.new was called,
>    # since a block is a closure
>
> end
>
>
> The second case is easy; it is covered by and_yield (with no arguments).
>
> Is there some way to spec the first case?  Do I smell the need for a
> patch?
>
> Scott
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list