What is the difference between Class and Klass in ruby?

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.


klass is also a Rails (or rather ActiveRecord) method. It is used for getting the class of an association.

As stated in the linked content:

class Author < ActiveRecord::Base
  has_many :books
end

Author.reflect_on_association(:books).klass
# => Book

klass and clazz and clasz and the like are creative misspellings used to get around a reserved word class.

It would be far less jarring on the eyes to use class1 or cl or classX or something rather than an intentional misspelling.


One is the name of a class and the other is just an undefined constant by default. And for the pair you're more likely to see, class and klass, the former is a keyword for defining classes while the latter is just an identifier (like any other string of characters). It's used when you would like to write the word "class" but can't because it's a reserved keyword.

Tags:

Ruby

Class