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

后端 未结 2 948
春和景丽
春和景丽 2021-02-02 15:11

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 me

相关标签:
2条回答
  • 2021-02-02 15:38

    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.

    0 讨论(0)
  • 2021-02-02 15:41

    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"
    
    0 讨论(0)
提交回复
热议问题