Last day while I was browing through some Java code online, I noticed that some method names it had were starting with capital letters. I have always used method names in ruby to be in small letter and use snake case. But it made me wonder if there was anything preventing us from using method names starting with capital letter, in ruby. So I started my irb and wrote the following code.
def Test puts 'testing 123' end
And when I ran to call the method
Test in the irb i got the following error.
NameError: uninitialized constant Test from (irb):4 from /Users/coderhs/.rvm/rubies/ruby-2.3.0/bin/irb:11:in `<main>'
But when I run the call as
Test() in the irb i got the expected output
So what I learned from this is that, when we start with capital it seems the interpretter
is looking for classes, modules and contants called
Test, and not looking for anything else. But when I add
the parathesis to the name, we are declaring that it is a method call and
hence the interpreter looks for a method called
PS: For those who might be wondering what would happen when we pass class name in all small letters
class testing def test puts 'testing' end end
You will get an error:
class/module name must be CONSTANT