vinod's erudition

we learn everything from failure not from success so keep failing

Home Ruby Rails Javascript Python Agentic AI

Metaprogramming Ruby fantabulous start

Posted on January 31, 2014 by vinod

####Metaprogramming Ruby

Oh my god!!!!!!! how I missed this book “Metaprogramming Ruby” the name was scarring me oh this may be the advanced version of ruby ,I should not read this until I am good enough in ruby but the Mr Paulo parretto made me clear by asking one question in introduction that made my mind that I am not still beginner in ruby that question was simple “how do you iterate an array ?” thank god I answered using “each” method and you may be surprised what’s in this to get surprised but yes there is big thing to get surprised and that is “if you answer using for loop array iteration can be done then you are not comfortable with ruby go back and kick the basics ass of ruby and come back ” and on other side “if you would have answered using “each” method array iteration can be done then you are not still beginner and you are the best to start kicking this book” .i hope now you would have concluded why I got surprised and excited.

After a fantabulous introduction of Metaprogramming Ruby, now its time to start the ride towards the wonderful island of Ruby and I started reading the first lesson ”Monday The Object Model” believe I was not having even one percent of feeling that I am reading a technical book. It was like a story revolving between Bob and Bill .where they both were involved in refactoring some library management system and here Bob is new to Ruby and Bill is a Ruby expert and most of the times Bill will be taking lead and teach Bob how to optimize aaahh the good word will be refactoring the code by teaching meta programming and Bill also demonstrates using some basic drawing for describing the hierarchy of Class and its super class as it goes on. come lets have a quick discussion about what I have learnt from the chapter1 “Monday The Object Model”.

####1.Monday The Object Model

Code:

class Test
def old_method (array,from,to) #old way to define a separate method to do replace operation

array.each_with_index do |x,y|
array[y] = to if x == from
end
end
end

class Array
def element_replace(from,to) #open the Array class and add element_replace method in to it.

self.each_with_index do |x,y|
self[y]=to if x==from
end
end
end
t=Test.new() #puts t.old_method([2,1,4,3],2,5)

puts [2,1,3,4].element_replace(2,5)

here in above example –

####Note:

[].methods.grep /^re/ #=>[:replace,:reject,..]

####What is an Object ?

####What is class? Class is an object which contains bunch of instance methods and links to superclass.

####What is instance methods? Instance methods are methods of object lives in object class from point of view of class.

####NOTE:

####Structure of Class

Sample code

module A
def my_name
Vinod
end
end

class B
include A
end

class C < B
end
C.new.my_name ”Vinod
C.ancestors [C,B,A,Object,kernel,BasicObject]

####Note:

####Where Modules and Class should be used?

####Execute Method

####SELF

####How to call Private Method?

code
class Test
private
def private_method
"this s private method"
end
end
class Sample < Test
    def call_private_test
		private_method
	end
end

s=Sample.new
puts s.call_private_test ”this is private method
puts s.private_method  private method cannot be accessed error

####TANGLE OF MODULES

Take a case that there are two modules (modules are King,Queen) of same method such as (name, age) and both modules are included in to a class(Lovers) and an object instantiated to the class calls a method called age.which method will get executed? I was stumbled when I read this and I guessed the it will execute the first module included here comes the twist of Ruby I will explain how Ruby fix this.

When first module King is included Ruby wraps in to class and adds it to ancestor chain above the base class (Lovers) assume we are considering a stack and last value is base class and Top value is BasicObject(Lovers,King,…BasicObject) .so when next module Queen gets include again ruby wraps in to class and push it above the base class(Lovers,Queen,King…BaseObject).

module King
	def name
		"vinod"
	end
	def age
		23
	end
end

module Queen
	def name 
		"katrina kaif"
	end
	def age
		27
	end
end

class Lovers
	include Queen
	include King
	def blast
		puts name
		puts age
	end
end

class Enemy
	include King
	include Queen
	def blast
		puts name
		puts age
	end
end
lov=Lovers.new
lov.blast
puts Lovers.ancestors #=>[Lovers,King,Queen,Object,Kernel,BasicObject]

puts Enemy.ancestors #=>[Lovers,Queen,King,Object,Kernel,BasicObject]