Chitra
Chitra

Reputation: 1404

What is the difference between `Class` and `class`

While creating a class, we use the keyword class like:

class Abc
  Z = 5
  def add
    puts "anything here"
  end
end

In console, Abc.class # => Class

How does Abc internally become a class? What is the difference between class and Class?

It would be great if anyone could explain how class constants and method are internally called, and if a method is not defined, then how we get the exception "undefined class method". What is the internal logic behind it?

Upvotes: 1

Views: 200

Answers (5)

ndnenkov
ndnenkov

Reputation: 36101

To see what the different "class" things in Ruby mean, check out my other answer.


As for how methods are looked up:

There are multiple places a method can come from:

  1. The class of the object
  2. The parent of the class of the object
  3. Modules, which are included/prepended
  4. The singleton class of the object

The order of lookup is the following:

  1. If it exists, the singleton class
  2. Prepended modules
  3. Methods defined in the class
  4. Included modules
  5. Recursively, the above rules for the parent class

There are a few things that have to be noted here:

  1. If multiple modules are included/prepended, they will be looked up in the reverse order of how they were included/prepended
  2. If a module was already included/prepended in one of the parrents, it won't be included/prepended again
  3. If using these rules the method was not found til the very start of the hierarchy (BasicObject), the ancestor chain is searched again for a different method, called method_missing
  4. BasicObject#method_missing is defined so that it throws a NoMethodError and that is where the error comes from

module M1
  def foo
    puts 'Defined in M1'
  end
end

module M2
  def foo
    puts 'Defined in M2'
  end
end

class C
  include M1
  prepend M2

  def foo
    puts 'Defined in C'
  end

  def method_missing(method_name)
    puts 'Method missing' if method_name == :foo
  end
end

c = C.new

# singleton method
def c.foo
  puts "Defined in c's singleton"
end

puts c.singleton_class.ancestors
  # => [#<Class:#<C:0xa2d0f8>>, M2, C, M1, Object, Kernel, BasicObject]
  #     ^ the singleton class,
  #       the prepended module,
  #       the C class itself,
  #       the included module,
  #       the rest of the hierarchy
  # (Object is the implicit parent of all classes with no explicit parent)

Upvotes: 1

Stefan
Stefan

Reputation: 114178

ndn's answer gives a nice overview of the different things "class" could refer to.

To answer your specific question:

How does Abc internally become a class?

Almost like any other object.

class Abc
end

is equivalent to:

Abc = Class.new do
end

It creates a new instance of Class and assigns it to the constant Abc.

Upvotes: 2

Borsunho
Borsunho

Reputation: 1157

Answering second par of the question, undefined class method happens when method you called is not present in such class - classes are just objects in Ruby, and as such they have their own set of methods. Ruby has several ways to define class methods, most common is probably

class Klass
  def self.klass_method
    ...
  end
end

the other is

class Klass
  #  some ordinary instance methods here
  class << self
    def klass_method
      # this is class method
    end
  end
end

Some Rubyists prefer the latter, as it keeps all class methods in single block, but they are equivalent.

Upvotes: 0

ndnenkov
ndnenkov

Reputation: 36101

There are three different things here:

  1. class is a keyword, which is used to define or reopen a class
  2. Object#class is a method, which returns the class of a given object
  3. Class is the class which all classes are an instance of (including Class itself)

Upvotes: 14

Tim Kretschmer
Tim Kretschmer

Reputation: 2280

with class Abc you define a class.

Abc.class is returning the Type, and the type of Abc is a Class

another example:

1337.class
=> Fixnum
"hi babe!".class
=> String
12.55.class
=> Float
(1..12).class
=> Range

so as you can see, each "datatype" is a class. in your case Abc is also a Datatype. And for the end of that chain, the class of a class is Class! :-)

Upvotes: 1

Related Questions