I have the following code in Ruby:
class Base
def Function1
puts 'Base Function1'
end
def Function2
Function1
end
end
class Derived < Base
def Function1
puts 'Derived Function1'
end
end
obj = Derived.new
obj.Function2
When I run the above code, I get the following error:
/Users/vkuppuswamy/.rvm/rubies/ruby-2.0.0-p0/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /Users/vkuppuswamy/RubymineProjects/TestRubyProj/TestRuby.rb
/Users/vkuppuswamy/RubymineProjects/TestRubyProj/TestRuby.rb:7:in `Function2': uninitialized constant Base::Function1 (NameError)
from /Users/vkuppuswamy/RubymineProjects/TestRubyProj/TestRuby.rb:18:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
I can see that the in Function2 in class Base, an attempt has been made to call some constant Function1. I do not see why this would happen. I thought the derived class Function1 method will be invoked. When I change the Function2 in the base class to:
def Function2
self.Function1
end
where I invoke Function1 using self, it works, and I get in output:
Derived Function1
Can you please help me understand why this is happening. I thought the self was implicit in Ruby.
function1etc. What happens?