Thursday, 26 April 2018

3 PILLARS OF RUBY, object oriented and metaprogramming

3 PILLARS OF RUBY

Following are the 3 pillars of Ruby

  1. Everything is an object.
  2. Every operation is a method call on some object.
  3. Everything is metaprogramming.

1. Everything is an object (Almost)

In ruby everything you see is an object.
Lets take an example:
irb(main):001:0> 2.to_s
=> "2"
irb(main):002:0> 'hello'.to_sym
=> :hello
Here 2 and hello is also an object.
So what is the class of 2 and ‘hello’?
Let’s find out
irb(main):005:0> 2.class
=> Fixnum
irb(main):006:0> ‘hello’.class
=> String
As you can see, even data types are the objects of some pre-defined classes.
What about nil?
Let’s find out
irb(main):009:0> nil.class
=> NilClass
Even nilis an object of class NilClass😅
Why (Almost)?
There are some things that are not purely object in Ruby like keywords like ifalias, and beginendrescue are not object in ruby.
Also, it’s worth noting that everything or every statement evaluates to an object.

2. Every operation is a method call on some object

Every operation you see is a method call on some object. Ruby provides you syntactic sugar to make the statement more intuitive. Let’s take some examples:
The simple statement
irb(main):001:0> 1 + 2
=> 3
can be re-written as
irb(main):001:0> 1.+(2)
=> 3
In this example we are calling + method on Fixnum object and passing 2 as an argument to it.
Some more examples:
1 + 2                    => 1.+(2) or 1.send(:+, 2)
my_array[4]              => my_array.[](4)
                            or my_array.send(:[], 4)
my_array[3] = "foo"      => my_array.[]=(3, ‘foo’)
if (x == 3) ....         => if (x.==(3))
my_func(z)               => self.my_func(z)
                            or self.send(:my_func, z)

3. Everything is metaprogramming.

Metaprogramming is a technique by which you can write code that writescode by itself dynamically at runtime. This means you can define methods and classes during runtime or you can manipulate methods and objects at run time.
The statement that everything is metaprogramming because even the class declaration or object initializations is the type of metaprogramming where you define new objects on existing objects.
Lets take an example:
If you go to the ruby console and run
irb(main):004:0> self.class
=> Object
you will see that self is the object of class Object. Now whatever you will define on console will become the part of Object class.
irb(main):021:0> def print_hello
irb(main):022:1> puts 'hello'
irb(main):023:1> end
=> :print_hello
irb(main):024:0> self.print_hello
hello
=> nil
As you can see, we have defined a method(called as singleton method)print_hello on pre-existing object self of class Object.
The same thing happens when you declare a class, for example:
irb(main):035:0> class Meta
irb(main):036:1> end
=> nil
irb(main):037:0> Meta.class
=> Class
Here, you are declaring a new object Meta of class Class.
That’s why what ever you write in ruby is meta-programming.
Wow!! that means you have been using Metaprogramming from the beginning without even knowing it. 😛

No comments:

Post a Comment

Followers