Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Class and Klass in ruby?

Tags:

class

ruby

People also ask

What is class and instance in Ruby?

In Ruby, a class is an object that defines a blueprint to create other objects. Classes define which methods are available on any instance of that class. Defining a method inside a class creates an instance method on that class. Any future instance of that class will have that method available.

Is a class an object in Ruby?

Object is the default root of all Ruby objects. Object inherits from BasicObject which allows creating alternate object hierarchies. Methods on Object are available to all classes unless explicitly overridden.

What is class New Ruby?

Classes in Ruby are first-class objects—each is an instance of class Class . Typically, you create a new class by using: class Name # some code describing the class behavior end. When a new class is created, an object of type Class is initialized and assigned to a global constant ( Name in this case).


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.