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.

1
2
3
def Test
  puts 'testing 123'
end

And when I ran to call the method Test in the irb i got the following error.

1
2
3
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 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:

1
2
3
4
5
class testing
  def test
    puts 'testing'
  end
end

You will get an error:

1
class/module name must be CONSTANT

I am sure there is a much more clearer explanation for this, but this is just my inferences from my experiment.