[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.

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 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:

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