Last active
December 15, 2015 12:29
-
-
Save sagarbommidi/5260843 to your computer and use it in GitHub Desktop.
Tricks and tips for meta programming in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage of class_eval method | |
class Sample | |
end | |
s = Sample.new | |
Sample.class_eval do | |
def say_hello | |
puts "I am becoming the instance method in Sample class" | |
end | |
end | |
Sample.class_eval do | |
def self.say_bye | |
puts "I am becoming the class method in Sample class" | |
end | |
end | |
# the following method will raise error. Because its not possible to execute class_eval for an object. | |
begin | |
s.class_eval do | |
def status | |
puts "I am no more executed." | |
end | |
end | |
rescue Exception => e | |
puts "Exception raised in above method defination" | |
end | |
s.say_hello # => I am becoming the instance method in Sample class | |
Sample.say_bye # => I am becoming the class method in Sample class | |
s.status # => This method was not created, so will raise NoMethodError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage of instance_eval method | |
class Sample | |
end | |
s = Sample.new | |
Sample.instance_eval do | |
def say_hello # or self.say_hello ; here both will create a class method in "Sample" class. | |
puts "Hello Meta Programming with Ruby" | |
end | |
end | |
s.instance_eval do | |
def status # or self.say_hello ; here both will create an instance method in "Sample" class. | |
puts "I am an instance method" | |
end | |
end | |
Sample.say_hello # => Hello Meta Programming with Ruby | |
s.status # => I am an instance method |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Getting/Setting instance variables | |
class Sample | |
def initialize | |
@secret = 99 | |
end | |
end | |
k = Sample.new | |
k.instance_variable_set('@secret', 21) | |
puts k.instance_variable_get('@secret') # => 21 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module EmailReporter | |
def send_report | |
puts "Email Reporter" | |
end | |
end | |
module PdfReporter | |
def send_report | |
puts "Pdf Reporter" | |
end | |
end | |
class Person | |
def send_report | |
puts "Person Reporter" | |
end | |
end | |
class User < Person | |
end | |
class Employee < Person | |
include EmailReporter | |
end | |
class Manager < Person | |
include EmailReporter | |
include PdfReporter | |
end | |
class Guest < Person | |
include PdfReporter | |
include EmailReporter | |
# the sequence of including the module will matter. | |
# the methods in EmailReporter will be looked up first when you called the send_reoprt on Guest object. | |
# because it is included recently. | |
end | |
Person.new.send_report # => Person Reporter | |
User.new.send_report # => Person Reporter | |
Employee.new.send_report # => Email Reporter | |
Manager.new.send_report # => Pdf Reporter | |
Guest.new.send_report # => Email Reporter |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment