Skip to content

Instantly share code, notes, and snippets.

@myobie
Created September 28, 2010 01:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save myobie/600227 to your computer and use it in GitHub Desktop.
Save myobie/600227 to your computer and use it in GitHub Desktop.
module Scope
module ScopeMethods
def helpers(&block)
instance_eval(&block)
end
end
def scope(path, &block)
(@path_parts ||= []) << path
context = eval('self', block.binding).dup
context.extend ScopeMethods
context.instance_eval(&block)
@path_parts.pop
end
end
include Scope
scope "something" do
helpers do
def foo
"bar"
end
end
puts foo # => should work
end
puts foo # => should error
@banister
Copy link

You need to refactor the scope() method to the following:

  def scope(path, &block)
      (@path_parts ||= []) << path

      context = eval('self', block.binding)
      dup_context = context.dup 
      dup_context.extend ScopeMethods 
      dup_context.instance_eval(&block)

       # update modified state
       context.instance_variables.each do |v|
          context.instance_variable_set(v, dup_context.instance_variable_get(v))
      end

     @path_parts.pop
end 

@banister
Copy link

Let me know if your tests pass with that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment