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')

Altri articoli interessanti:

  1. Google Visualr: libreria Ruby per Google Visualization API
  2. RVM: Installare tante versioni di Ruby e tante versioni di Rails
  3. IronRuby: Ruby e Rails su piattaforma .NET

Questo articolo è stato pubblicato in Web e ha le etichette , , , . Aggiungi ai preferiti: link permanente. I commenti sono chiusi ma puoi lasciare un trackback: Trackback URL.
  • Inserisci il tuo indirizzo di posta per ricevere i prossimi articoli gratis:


    Servizio gestito tramite Feedburner