Skip to content

Commit

Permalink
add instance_exec for Ruby 1.8.6
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Aug 2, 2010
1 parent cc72146 commit 7d492bd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rspec/core.rb
@@ -1,4 +1,5 @@
require 'rspec/core/kernel_extensions'
require 'rspec/core/instance_exec'
require 'rspec/core/object_extensions'
require 'rspec/core/load_path'
require 'rspec/core/deprecation'
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/core/example_group.rb
Expand Up @@ -2,6 +2,7 @@ module RSpec
module Core
class ExampleGroup
extend Hooks
include InstanceExec
include Subject
include Let
include Pending
Expand Down
30 changes: 30 additions & 0 deletions lib/rspec/core/instance_exec.rb
@@ -0,0 +1,30 @@
module RSpec
module Core
module InstanceExec
unless respond_to?(:instance_exec)
# based on Bounded Spec InstanceExec (Mauricio Fernandez)
# http://eigenclass.org/hiki/bounded+space+instance_exec
# - uses singleton_class instead of global InstanceExecHelper module
# - this keeps it scoped to classes/modules that include this module
# - only necessary for ruby 1.8.6
def instance_exec(*args, &block)
singleton_class = (class << self; self; end)
begin
orig_critical, Thread.critical = Thread.critical, true
n = 0
n += 1 while respond_to?(method_name="__instance_exec#{n}")
singleton_class.module_eval{ define_method(method_name, &block) }
ensure
Thread.critical = orig_critical
end
begin
return send(method_name, *args)
ensure
singleton_class.module_eval{ remove_method(method_name) } rescue nil
end
end
end
end
end
end

0 comments on commit 7d492bd

Please sign in to comment.