PageRenderTime 18ms CodeModel.GetById 13ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/bundle/jruby/2.1/gems/rspec-core-2.14.8/lib/rspec/core/extensions/instance_eval_with_args.rb

https://github.com/delowong/logstash
Ruby | 44 lines | 26 code | 2 blank | 16 comment | 4 complexity | d9f8de682dfcb43e2b5345877f111046 MD5 | raw file
 1module RSpec
 2  module Core
 3    module Extensions
 4      # @private
 5      module InstanceEvalWithArgs
 6        # @private
 7        #
 8        # Used internally to support `instance_exec` in Ruby 1.8.6.
 9        #
10        # based on Bounded Spec InstanceExec (Mauricio Fernandez)
11        # http://eigenclass.org/hiki/bounded+space+instance_exec
12        # - uses singleton_class instead of global InstanceExecHelper module
13        # - this keeps it scoped to classes/modules that include this module
14        # - only necessary for ruby 1.8.6
15        def instance_eval_with_args(*args, &block)
16          return instance_exec(*args, &block) if respond_to?(:instance_exec)
17
18          # If there are no args and the block doesn't expect any, there's no
19          # need to fake instance_exec with our hack below.
20          # Notes:
21          #   * lambda { }.arity # => -1
22          #   * lambda { || }.arity # => 0
23          #   * lambda { |*a| }.arity # -1
24          return instance_eval(&block) if block.arity < 1 && args.size.zero?
25
26          singleton_class = (class << self; self; end)
27          begin
28            orig_critical, Thread.critical = Thread.critical, true
29            n = 0
30            n += 1 while respond_to?(method_name="__instance_exec#{n}")
31            singleton_class.module_eval{ define_method(method_name, &block) }
32          ensure
33            Thread.critical = orig_critical
34          end
35          begin
36            return send(method_name, *args)
37          ensure
38            singleton_class.module_eval{ remove_method(method_name) } rescue nil
39          end
40        end
41      end
42    end
43  end
44end