Thursday 19 July 2012

Struct && delegate

Person = Struct.new(:name, :address)

class Invoice < Struct.new(:client)
  delegate :name, :address, :to => :client, :prefix => true
end

john_doe = Person.new("John Doe", "Vimmersvej 13")
invoice = Invoice.new(john_doe)
invoice.client_name    # => "John Doe"
invoice.client_address # => "Vimmersvej 13"

No comments:

Post a Comment