ruby中Class和Klass有什么区别? [英] What is the difference between Class and Klass in ruby?

查看:568
本文介绍了ruby中Class和Klass有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Class和Klass有什么区别?

解决方案

class 是用于定义新类的关键字。由于它是保留关键字,因此您无法将其用作变量名称。你不能使用任何Ruby的关键字作为变量名,所以你将不能有变量名为 def 模块 if end ,etc - class



例如,请考虑以下内容:

  def show_methods(class)
puts Object.const_get(class).methods.inspect
end
show_methodsKernel

尝试运行此操作会导致错误,因为您不能使用 class 作为变量名称。

  test.rb:1:语法错误,意外的kCLASS,期望')'
def show_methods(class)
^
test.rb:2:语法错误,意外')'
puts Object.const_get(class).methods.inspect

要修复它,我们将使用标识符 klass 。它不是特殊的,但它在处理类或类名时通常用作变量名。它在语音上是一样的,但因为它不是一个保留的关键字,Ruby没有问题。

  def show_methods(klass)
puts Object.const_get(klass).methods.inspect
end

show_methodsKernel

正如预期,输出是

  [method,inspect name,public_class_method,chop!... 

保留)变量名,但是社区已经使用 klass 。它没有任何特殊的魔法 - 它只是意味着我想在这里使用名称'类',但我不能,因为它是一个保留关键字。



<在旁注,因为你输入错了几次,值得注意的是,在Ruby中,情况重要。以大写字母开头的令牌是常量。通过鹤嘴锄


常量名称以大写字母开头,后跟名称字符。类名和模块名称是常量,并遵循常量命名约定。按照惯例,常量变量通常拼写时使用大写字母和下划线。


因此,正确的拼写是 klass code>。后者将是常量,并且 Class Klass 都是有效的常量名,但我建议不要使用它们清晰的目的。


What is the difference between Class and Klass?

解决方案

class is a keyword used to define a new class. Since it's a reserved keyword, you're not able to use it as a variable name. You can't use any of Ruby's keywords as variable names, so you won't be able to have variables named def or module or if or end, etc - class is no different.

For example, consider the following:

def show_methods(class)
  puts Object.const_get(class).methods.inspect
end
show_methods "Kernel"

Trying to run this results in an error, since you can't use class as a variable name.

test.rb:1: syntax error, unexpected kCLASS, expecting ')'
    def show_methods(class)
                          ^
test.rb:2: syntax error, unexpected ')'
      puts Object.const_get(class).methods.inspect

To fix it, we'll use the identifier klass instead. It's not special, but it's conventionally used as a variable name when you're dealing with a class or class name. It's phonetically the same, but since it's not a reserved keyword, Ruby has no issues with it.

def show_methods(klass)
  puts Object.const_get(klass).methods.inspect
end

show_methods "Kernel"

Output, as expected, is

["method", "inspect", "name", "public_class_method", "chop!"...

You could use any (non-reserved) variable name there, but the community has taken to using klass. It doesn't have any special magic - it just means "I wanted to use the name 'class' here, but I can't, since it's a reserved keyword".

On a side note, since you've typed it out wrong a few times, it's worth noting that in Ruby, case matters. Tokens that start with a capital letter are constants. Via the Pickaxe:

A constant name starts with an uppercase letter followed by name characters. Class names and module names are constants, and follow the constant naming conventions. By convention, constant variables are normally spelled using uppercase letters and underscores throughout.

Thus, the correct spelling is class and klass, rather than Class and Klass. The latter would be constants, and both Class and Klass are valid constant names, but I would recommend against using them for clarity purposes.

这篇关于ruby中Class和Klass有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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