I've came across an issue with instance_eval and module inclusion.
Please take a look at the following code:
module B
class C
def initialize
puts 'This is C'
end
end
def hello
puts 'hello'
end
end
class A
include B
def initialize(&block)
hello
C.new
instance_eval(&block)
end
end
A.new do
hello
C.new
end
When I run this code I get
hello
This is C
hello
-:25:in `block in ': uninitialized constant C (NameError)
from -:19:in `instance_eval'
from -:19:in `initialize'
from -:23:in `new'
from -:23:in `'
I understand that it has to do with bindings and how methods and objects are binded to class. What I cannot understand is how come I have access to C within A, but not when I eval the block. I would expect them to be in the same scope.
Thanks!
In the below code
You are are trying to create the object of
C, as if it is defined in the scope of the classObject. No, it is not. classCis defined inside the scope of moduleB. And you need to tell that explicitly asB:C.new.Above explanation is for the error -:25:in 'block in ': uninitialized constant C (NameError).
Module#constantshas the answer for you :-Look at the example :
Now applying to your example :
Hope that helps.