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 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
Duh. I should be using and_return(), not and_yield(), since I am actually returning the value of the instance eval. The method in question takes one parameter, a proc obj (as block). But how can I get a handle on that object (since it is anonymous)? Scott On Aug 7, 2007, at 9:14 PM, Scott Taylor 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 > > 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
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 > endWhy 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 >
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 > > 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?I''m not sure what you''re trying to accomplish here - what would this spec look like?> > Scott > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
On Aug 10, 2007, at 3:43 PM, David Chelimsky wrote:> 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 { ... }Maybe a better example would be in place. Consider the following from the Autotest plugin: Autotest.add_discovery do "rspec" if File.exist?(''spec'') end How would you go about testing this? It''s easy enough to test that Autotest receives the method add_discovery. But how would you deal with the block? Scott
On 8/10/07, Scott Taylor <mailing_lists at railsnewbie.com> wrote:> > On Aug 10, 2007, at 3:43 PM, David Chelimsky wrote: > > > 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 { ... } > > Maybe a better example would be in place. Consider the following > from the Autotest plugin: > > Autotest.add_discovery do > "rspec" if File.exist?(''spec'') > end > > How would you go about testing this? > > It''s easy enough to test that Autotest receives the method > add_discovery. But how would you deal with the block?OK - now I''m starting to see. Concrete examples are always helpful. I guess there''s no mocking framework right now that would solve that for you. What spec do you *wish* you could write?