/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

  1. module 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. # If there are no args and the block doesn't expect any, there's no
  18. # need to fake instance_exec with our hack below.
  19. # Notes:
  20. # * lambda { }.arity # => -1
  21. # * lambda { || }.arity # => 0
  22. # * lambda { |*a| }.arity # -1
  23. return instance_eval(&block) if block.arity < 1 && args.size.zero?
  24. singleton_class = (class << self; self; end)
  25. begin
  26. orig_critical, Thread.critical = Thread.critical, true
  27. n = 0
  28. n += 1 while respond_to?(method_name="__instance_exec#{n}")
  29. singleton_class.module_eval{ define_method(method_name, &block) }
  30. ensure
  31. Thread.critical = orig_critical
  32. end
  33. begin
  34. return send(method_name, *args)
  35. ensure
  36. singleton_class.module_eval{ remove_method(method_name) } rescue nil
  37. end
  38. end
  39. end
  40. end
  41. end
  42. end