Module: RSpec::Core::Extensions::ModuleEvalWithArgs

Includes:
InstanceEvalWithArgs
Included in:
RSpec::Core::ExampleGroup
Defined in:
lib/rspec/core/extensions/module_eval_with_args.rb

Instance Method Summary collapse

Methods included from InstanceEvalWithArgs

#instance_eval_with_args

Instance Method Details

#module_eval_with_args(*args, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rspec/core/extensions/module_eval_with_args.rb', line 7

def module_eval_with_args(*args, &block)
  # ruby > 1.8.6
  return module_exec(*args, &block) if respond_to?(:module_exec)

  # If there are no args and the block doesn't expect any, there's no
  # need to fake module_exec with our hack below.
  # Notes:
  #   * lambda {      }.arity # => -1
  #   * lambda { ||   }.arity # =>  0
  #   * lambda { |*a| }.arity # => -1
  return module_eval(&block) if block.arity < 1 && args.size.zero?

  orig_singleton_methods = singleton_methods
  instance_eval_with_args(*args, &block)

  # The only difference between instance_eval and module_eval is static method defs.
  #   * `def foo` in instance_eval defines a singleton method on the instance
  #   * `def foo` in class/module_eval defines an instance method for the class/module
  # Here we deal with this difference by defining an instance method for
  # each new singleton method.
  # This has the side effect of duplicating methods (all new class methods will
  # become instance methods and vice versa), but I don't see a way around it...
  (singleton_methods - orig_singleton_methods).each { |m| define_method(m, &method(m)) }
end