[TIL] Starting Ruby Method With Capital Letter
Last day while I was browsing through some Java code online, I noticed that some method has names starting with capital letters. I Method names are written in small letters in ruby. This Java code 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 was that, when we start with capital it seems the interpreter is looking for classes, modules and constants called Test, and not looking for anything else. Thus giving you the error.
When I add the parenthesis to the name, we are doing 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:
|
|
I am sure there is a much more clearer explanation for this, but this is just my inferences from my experiment.