What is the difference between class and class in ruby?

What is the difference between class and class?

+84
ruby class
Nov 28 2018-10-11T00:
source share
4 answers

class is a keyword used to define a new class. Since this is a reserved keyword, you cannot use it as a variable name. You cannot use any of the Ruby keywords as variable names, so you cannot have variables named def or module or if or end , etc. class no different.

For example, consider the following:

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

Attempting to run this results in an error, since you cannot use class as the name of a variable.

 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 this, we will use the klass identifier instead. This is not special, but it is usually used as a variable name when you are dealing with a class or class name. This is phonetically the same, but since it is not a reserved keyword, Ruby has no problem with it.

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

The output, as expected,

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

You can use any (not reserved) variable name there, but the community has accepted the use of klass . He has no special magic - it just means that "I wanted to use the name" class "here, but I can’t, because this is a reserved keyword."

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

The name of the constant begins with a capital letter, followed by the characters of the name. Class names and module names are constants and follow constant naming conventions. By convention, constant variables are usually written in capital letters and underscores.

Thus, the correct spelling of class and klass , not class and klass . The latter would be constants, and both class and klass were valid constant names, but I would recommend using them for clarity.

+147
Nov 28 '10 at
source share

klass and clazz and clasz , etc. are creative bugs used to bypass the reserved word class .

It would be much less annoying to use class1 or cl or classX or something, rather than intentional spelling.

+9
Apr 28 2018-11-21T00:
source share

I think klass also a Rails method (or rather an ActiveRecord). It is used to get an association class.

+9
Jan 27 '14 at 13:09
source share

One of them is the class name, and the other is the undefined constant by default. And for the pair that you are likely to see, class and klass , the first is a keyword for defining classes, and the last is just an identifier (like any other character string). It was used when you would like to write the word "class", but you cannot, because it is a reserved keyword.

+5
Nov 28 2018-10-28T00-11-28
source share



All Articles