Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the uppercase and lowercase rules of ruby method name?

Tags:

ruby

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:

  1. 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 not work:

    define SayHi
      puts "Hello" 
    end
    SayHi 
    

    it will produce an error:

    :in `<main>': uninitialized constant SayHi (NameError)
    
  2. 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?

like image 462
Steve Zhang Avatar asked Jan 09 '10 01:01

Steve Zhang


2 Answers

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"
like image 115
John Feminella Avatar answered Nov 16 '22 02:11

John Feminella


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.

like image 26
Bob Aman Avatar answered Nov 16 '22 03:11

Bob Aman