[TIL] Starting Ruby Method With Capital Letter
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.
|
|
And when I ran to call the method Test in the irb i got the following error.
|
|
But when I run the call as Test() in the irb i got the expected output testing 123.
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 Test
PS: For those who might be wondering what would happen when we pass class name in all small letters
eg:
|
|
You will get an error:
|
|