Name: Thaichor Seng Position: Junior Developer at Yoolk.Inc I‟m currently working on creating Restaurant Menu application using Ruby on Rails.
• Sugar Syntax • Dynamic Language • English Like • Lots of built-in methods • Metaprogramming
Metaprogramming is writing code that writes code. Here are some method using for metaprogramming: • define_method() • method_missing() • class_eval() • eval()
class MyClass define_method :my_method do |my_arg| my_arg * 3 end # def my_method(my_arg) # my_arg * 3 # end end obj = MyClass.new obj.my_method(2) # => 6
class Currency def initialize(value) @value = value end [:usd, :riel, :yen, :bat].each do |method| define_method “to_#{method}” do “#{@value.to_s} #{method.to_s}” end end end c = Currency.new(14) c.to_usd # 14 usd c.to_riel # 14 riel
class Lawyer end nick = Lawyer.new nick.talk_simple NoMethodError: undefined method „talk_simple' for #<Lawyer:0x3c848>
class Lawyer def method_missing(method, *args) puts "You called: #{method}(#{args.join(', ')})" end end nick = Lawyer.new nick.talk_simple # You called talk_simple() Nick.talk(a, b) # You called talk(a, b)
class MyOpenStruct def initialize @attributes = {} end def method_missing(name, *args) attribute = name.to_s if attribute =~ /=$/ @attributes[attribute.chop] = args[0] else @attributes[attribute] end end end icecream = MyOpenStruct.new icecream.flavor = "vanilla“ # nil icecream.flavor # vanilla
def add_method_to(a_class) a_class.class_eval do def m 'Hello!' end end end add_method_to String "abc".m # => "Hello!“
map = { "update" => "deploy:update" , "restart" => "deploy:restart" , "cleanup" => "deploy:cleanup" } map.each do |old, new| eval "task(#{old.inspect}) do warn "[DEPRECATED] `#{old}' is deprecated. Use `#{new}' instead." find_and_execute_task(#{new.inspect}) end" end
The End

Ruby Metaprogramming

  • 2.
    Name: Thaichor Seng Position:Junior Developer at Yoolk.Inc I‟m currently working on creating Restaurant Menu application using Ruby on Rails.
  • 3.
    Sugar Syntax • Dynamic Language • English Like • Lots of built-in methods • Metaprogramming
  • 4.
    Metaprogramming is writingcode that writes code. Here are some method using for metaprogramming: • define_method() • method_missing() • class_eval() • eval()
  • 5.
    class MyClass define_method :my_method do |my_arg| my_arg * 3 end # def my_method(my_arg) # my_arg * 3 # end end obj = MyClass.new obj.my_method(2) # => 6
  • 6.
    class Currency def initialize(value) @value = value end [:usd, :riel, :yen, :bat].each do |method| define_method “to_#{method}” do “#{@value.to_s} #{method.to_s}” end end end c = Currency.new(14) c.to_usd # 14 usd c.to_riel # 14 riel
  • 7.
    class Lawyer end nick =Lawyer.new nick.talk_simple NoMethodError: undefined method „talk_simple' for #<Lawyer:0x3c848>
  • 8.
    class Lawyer def method_missing(method, *args) puts "You called: #{method}(#{args.join(', ')})" end end nick = Lawyer.new nick.talk_simple # You called talk_simple() Nick.talk(a, b) # You called talk(a, b)
  • 9.
    class MyOpenStruct def initialize @attributes = {} end def method_missing(name, *args) attribute = name.to_s if attribute =~ /=$/ @attributes[attribute.chop] = args[0] else @attributes[attribute] end end end icecream = MyOpenStruct.new icecream.flavor = "vanilla“ # nil icecream.flavor # vanilla
  • 10.
    def add_method_to(a_class) a_class.class_eval do def m 'Hello!' end end end add_method_to String "abc".m # => "Hello!“
  • 11.
    map = {"update" => "deploy:update" , "restart" => "deploy:restart" , "cleanup" => "deploy:cleanup" } map.each do |old, new| eval "task(#{old.inspect}) do warn "[DEPRECATED] `#{old}' is deprecated. Use `#{new}' instead." find_and_execute_task(#{new.inspect}) end" end
  • 12.