Rubino può essere spaventoso semplice a volte. Nessun loop in vista!
class Weekend < Struct.new(:start_date, :end_date, :title, :description, :location)
# params: Hash with symbols as keys
def initialize(params)
# arg splatting to the rescue
super(* params.values_at(* self.class.members))
end
end
Si noti che non hanno nemmeno bisogno di usare l'ereditarietà - un nuovo Struct
può essere personalizzato durante la creazione:
Weekend = Struct.new(:start_date, :end_date, :title, :description, :location) do
def initialize(params)
# same as above
end
end
prova:
weekend = Weekend.new(
:start_date => 'start_date value',
:end_date => 'end_date value',
:title => 'title value',
:description => 'description value',
:location => 'location value'
)
p [:start_date , weekend.start_date ]
p [:end_date , weekend.end_date ]
p [:title , weekend.title ]
p [:description, weekend.description ]
p [:location , weekend.location ]
Si noti che questo non lo fa imposta effettivamente variabili di istanza. La tua classe avrà getter e setter opachi. Se preferisci non esporli, puoi avvolgere un'altra classe intorno ad essa. Ecco un esempio:
# this gives you more control over readers/writers
require 'forwardable'
class Weekend
MyStruct = ::Struct.new(:start_date, :end_date, :title, :description, :location)
extend Forwardable
# only set up readers
def_delegators :@struct, *MyStruct.members
# params: Hash with symbols as keys
def initialize(params)
# arg splatting to the rescue
@struct = MyStruct.new(* params.values_at(* MyStruct.members))
end
end
Odio rispondere da iPhone e vedere markup viziato ... – apneadiving
Capisco il desiderio di evitare metaprog inutili, ma questo non è il codice sintetico che l'OP stava chiedendo. Devi specificare manualmente ciascun nome di campo. – Kelvin