What are the uppercase and lowercase rules of ruby method name?
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 # => Hello
but, the following code does n开发者_运维知识库ot work:
define SayHi puts "Hello" end SayHi
it 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?
By convention, things that start with uppercase letters are constants. When you invoke SayHi
, you're telling Ruby to look for a constant with this name. Of course, there isn't one, so it fails.
If you want to invoke the method, you'll need to add a pair of parentheses. For example,
def S
puts "shazam!"
end
S #=> NameError: uninitialized constant S
S() #=> "shazam!"
Inside of a class, the resolution rules are a little different. Let's define a simple class with a constant and a method named to look like a constant:
irb(main):001:0> class C
irb(main):002:1> A = "x"
irb(main):003:1> def B
irb(main):004:2> puts "B() invoked"
irb(main):005:2> end
irb(main):006:1> end
=> nil
Now, A
is certainly a constant. But what about B
?
irb(main):008:0> C.const_defined?("A")
=> true # A is a constant!
irb(main):009:0> C.const_defined?("B")
=> false # B looks like a constant but isn't, because we
# used "def" to create it. "def" is for methods,
# not constants.
So it isn't a constant, just a method with that name. When we try to access B
from an instance of C
, now Ruby's looking for a method:
irb(main):011:0> C.new.B
B() invoked
=> nil
If we wanted to access a constant of C
instead, we use the scope qualifier ::
:
irb(main):012:0> C::A
=> "x"
Don't do any of what you're trying to do. It's bad coding style.
Good Ruby coding style:
def method_name
end
# or
class CamelCase
def method_name(parameter_name)
end
end
Almost anything else is simply wrong. The language might let you do other stuff, but that doesn't mean you should.
Also, generally you don't want to be defining methods outside of a class or module — that's acceptable in short throw-away scripts, but not in projects of substance.
精彩评论