Ruby object model

2 Min. Read
Dec 28, 2019

 Ruby Ruby Object Model Objects OOP

Object Introduction

Object is the base of the idea of Object Oriented Programming. Object is the mapping of real world objects or resources in to the digital paradigm.

Ruby is a true object oriented language because everything in Ruby is an object. Objects are the first-class citizens of the Ruby world. Even language constructs such as classes, instance variables, methods and modules are also objects. They live in a system known as Object Model. What do you think an object is? It’s just a bunch of instance variables, plus a link to a class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# numbers are objects
num  = 1
num.object_id

chr = 'a'
chr.object_id

# classes are also objects
class.object_id

# Even methods are objects
method = num.method(:times)
method.object_id

# operators are also objects
# actually they are methods defined in an object and which are also objects
# see all the methods `num.methods`
plus = num.method(:+)
plus.object_id

Object Model

Object model shivabhusal.com


So basically, Object Model is a world or system where all these language constructs live together. Object Model is where you will find answers to the questions like “which class does this method come from?” , “what happens when I inherit this class?” and “what instance variables does this object have?”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class A; end
class B < A; end

B = B.new
B.class
B.ancestors
B.superclass
# modules do not fall in `superclass`

# which class does this method come from?
class A
  def new_method
     print new method
  end
end


class B < A; end

b= B.new
b.new_method
> b.method :new_method
=> #<Method: B(A)#new_method>

what instance variables does this object have?
b.instance_variables
# []

Introduction to Class

The object’s methods don’t live in the object—they live in the object’s class, where they’re called the instance methods of the class.

What’s a class?

It’s just an object (an instance of Class), plus a list of instance methods and a link to a superclass. Class is a subclass of Module, so a class is also a module.

Like any object, a class has its own methods, such as new( ) and other you define. These are instance methods of the Class class. They are also called class-methods. You define these methods using self. prefix.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# reference
class A
    # this method is local to this particular class
    # other descendants of the Class class don’t have it
    # this is singleton method
    def self.some_method
        print some method
    end
end

A.singleton_methods
# [:some_method]

A.some_method
a=A.new
a.some_method # is not defined

Also like any object, classes must be accessed through references. You already have a constant reference to each class i.e. the class’s name.

You might also like

The most easiest way to learn React and more easier way to learn how to use Redux with React
Read More..

How To Apply For Adding Category To Driving License In Nepal Online 

ShivaShiva Bhusal (Software Engineer) 0 Min. Read Feb 16, 2020

Its easy to apply for adding category driving license online here in Nepal. Just goto the link mentioned with mentioned document on time.
Read More..

Ruby Object Model 

ShivaShiva Bhusal (Software Engineer) 2 Min. Read Dec 28, 2019
 Ruby Ruby Object Model Objects OOP

Object is the base of the idea of Object Oriented Programming. Object is the mapping of real world objects or resources in to the digital paradigm.
Read More..


Configure Rails Admin With I18n 

ShivaShiva Bhusal (Software Engineer) 1 Min. Read Aug 8, 2017
 Ruby On Rails Ruby On Rails Admin Configure I18n

By default, its not that easy to configure your application with i18n. If you wish to support your local language like `esp`, `np`, `hi`, `mx`, etc. you need to follow the steps i tell you.
Read More..


Write To Me

Hire me shiva bhusal
We'll never share your email with anyone else.
I'll never share your email with anyone else.