Metaprogrammazione in Ruby

class Wrapper
  attr_accessor :internal

  def self.forwards(*methods)
    [*methods].each do |method|
      class_eval("
        def #{method}(*args, &blk)
          self.internal.send(#{method.to_sym.inspect}, *args, &blk)
        end
      ")
    end
  end

  forwards :to_i, :length, :split
end

w = Wrapper.new
w.internal = "12 13 14"
puts w.to_i
puts w.length
puts w.split('1')

5 Ottobre 2008
Categorie: Uncategorized

AddThis Social Bookmark Button

Articoli simili

Commenti

Comments are closed.