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.