On Friday, April 25, 2014 7:22:38 AM UTC-7, John Feminella wrote:
>
> I'm asking this question here because it occurred to me that RSpec seems 
> to do what I want, but I can't figure out how to replicate this effect in 
> my own code. 
>
> Inside the body of a class, I'd like to pass a block to a method called 
> `with`. For the lifetime of the block, I would like a `with_value` method 
> to be available. Otherwise, everything inside the block should behave as if 
> it were outside the block.
>
> Here's an example:
>
>     class C
>       extend M
>     
>       with "some value" do
>         do_something_complicated
>         do_something_complicated
>         do_something_complicated
>       end
>     end
>
> We can _almost_ get this with:
>
>     module M
>       def with(str, &block)
>         Object.new.tap do |wrapper|
>           wrapper.define_singleton_method :with_value do  # Here's our 
> with_value
>             str                                           # method.
>           end
>         end.instance_eval &block
>       end
>     
>       def do_something_complicated                        # Push a value 
> onto an
>         (@foo ||= []).push with_value                     # array.
>       end
>     end
>
> but there's a problem: since we're evaluating the block passed to `with` 
> inside the context of a different object, `do_something_complicated` isn't 
> available.
>
> What's the right way to pull this off? Any suggestions?
>

RSpec doesn't do anything fancy with blocks....just calls them and/or 
`instance_eval`s them occasionally.  It's not clear to me which part of 
RSpec you are thinking of? 

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/24cfa121-090d-468a-8bd4-a2c34336021e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to