Reputation: 85
I want to use a method c of class B. For that class A inherits class B inside the module M. Now, how can I create an object of class A and call c?
module M
class A<B
def C
puts "From A"
end
end
class B
def C
puts "From B"
end
end
end
I am getting error " uninitialized constant M::B (NameError)"
I am unable to create object and call c like this:
ob=M::A.new
ob.C
Upvotes: 0
Views: 731
Reputation: 51171
You implement class A
that you want to inherit from B
before class B
. Change this sequence and it will work:
module M
class B
def c
puts "From B"
end
end
class A < B
def c
puts "From A"
end
end
end
obj = M::A.new
ob.c
# From A
I also corrected method named with capital letter. It's possible, but not recommended.
Upvotes: 1
Reputation: 11004
If you run this example, you are getting the following error:
NameError: uninitialized constant M::B
So B
is not found. As soon as you add it to your code, everything works fine:
class B
end
module M
class A < B
def c
puts "Test"
end
end
end
ob = M::A.new
ob.c
# >> Test
Upvotes: 0