ruby 方法名的大小写规则是什么? [英] What are the uppercase and lowercase rules of ruby method name?

查看:48
本文介绍了ruby 方法名的大小写规则是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Ruby 初学者.从这本书中,我知道 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. 如果一个方法是在类之外定义的,它只能以小写字母开头,如果你尝试定义一个以大写字母开头的方法,Ruby会报错,例如:

  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 

它会产生一个错误:

: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
    

  • 有谁知道为什么#1 不起作用而#2 起作用?ruby 方法名称的确切规则是什么?

    Does anyone know why #1 does not work while #2 work? What are the exact rules the ruby method name?

    推荐答案

    按照惯例,以大写字母开头的东西是常量.当您调用 SayHi 时,您是在告诉 Ruby 查找具有此名称的常量.当然,没有,所以失败了.

    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
    

    现在,A 肯定是一个常数.但是B呢?

    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.
    

    所以它不是一个常量,只是一个具有该名称的方法.当我们尝试从 C 的实例访问 B 时,现在 Ruby 正在寻找一个方法:

    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
    

    如果我们想访问 C 的常量,我们使用范围限定符 :::

    If we wanted to access a constant of C instead, we use the scope qualifier :::

    irb(main):012:0> C::A
    => "x"
    

    这篇关于ruby 方法名的大小写规则是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆