I am a Ruby beginner. From the book, I know that a Ruby method name should start with a lowercase letter or underscore. But I found different scenarios:
If a method is defined outside a class, it can only begin with lowercase letter, Ruby will complain with an error if you try to define a method which begins with an uppercase letter, for example:
define sayHi puts "Hello" end sayHi # => Hellobut, the following code does not work:
define SayHi puts "Hello" end SayHiit will produce an error:
:in `<main>': uninitialized constant SayHi (NameError)If a method is defined inside a class, then it can begin with uppercase letter:
class Test def SayHi puts "hello" end end t = Test.new t.SayHi # => hello
Does anyone know why #1 does not work while #2 work? What are the exact rules the ruby method name?