Reputation: 1404
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
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:
The order of lookup is the following:
There are a few things that have to be noted here:
BasicObject
), the ancestor chain is searched again for a different method, called method_missing
BasicObject#method_missing
is defined so that it throws a NoMethodError
and that is where the error comes frommodule 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
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
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
Reputation: 36101
There are three different things here:
class
is a keyword, which is used to define or reopen a classObject#class
is a method, which returns the class of a given objectClass
is the class which all classes are an instance of (including Class
itself)Upvotes: 14
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