Created
October 19, 2010 08:58
-
-
Save bagwanpankaj/633877 to your computer and use it in GitHub Desktop.
Ruby MetaProgramming Samples
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
class Testt | |
define_method :foo do | |
"welcome" | |
end | |
end | |
Testt.new().foo | |
=> "welcome" | |
module Hello | |
define_method :foo do |name| | |
"welcome #{name}" | |
end | |
end | |
include Hello | |
foo "Dave" | |
=> "welcome Dave" |
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
Testt.instance_eval do | |
def foo | |
"I'll be available as class method" | |
end | |
def self.bar | |
"I'll be available as class method" | |
end | |
end | |
Testt.foo | |
=>"I'll be available as class method" | |
Testt.bar | |
=>"I'll be available as class 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
class Testt | |
end | |
Testt.class_eval do | |
def foo | |
"I'll be available as instance method" | |
end | |
def self.foo | |
"I'll be available as class method" | |
end | |
end | |
Testt.new().foo | |
=> "I'll be available as instance method" | |
Testt.foo | |
=>"I be available as class 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
class User < ActiveRecord::Base | |
#user has one role so association is | |
has_one :role | |
#First time when there is no method found; this method will catch that | |
def method_missing(method_sym, *params) | |
#Here we match the pattern of method and see if we can process it or not | |
if method_sym.to_s =~ /^is_(.*)\?$/ | |
function = $1 | |
self.class.class_eval <<-END | |
def #{method_sym.to_s} | |
#{role.name == function.to_s.upcase} | |
end | |
END | |
#Let's register this method with user class when it is invoked first time. | |
send(method_sym) | |
else | |
super | |
end | |
end | |
#Let this class respond for these methods | |
def self.respond_to?(method_sym, *args, &block) | |
method_sym.to_s =~ /^is_(.*)\?$/ ? true : super | |
end | |
end |
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
class User < ActiveRecord::Base | |
#user has one role so association is | |
has_one :role | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment