Sam Kong ha scritto: > Hi! > > I'm creating a class. > I wonder if there's a better way. > > require 'dbi' > > class Person > attr_accessor :person_id, > :last_name, > :first_name, > :home_phone, > :work_phone, > :address, > :city, > :state, > :zip > > def initialize(row=nil) > if row.class == DBI::Row then > @person_id = row.by_field("person_id") > @last_name = row.by_field("last_name") > @first_name = row.by_field("first_name") > @home_phone = row.by_field("home_phone") > @work_phone = row.by_field("work_phone") > @address = row.by_field("address") > @city = row.by_field("city") > @state = row.by_field("state") > @zip = row.by_field("zip") > end > end > end > > The code in initialize method is very boring. > How could I make it simpler? > > Thanks in advance. > > Sam > I think I'd do the initialize method like this: def initialize(row=nil) if row.is_A? DBI::Row row.each_with_name do |value,name| send name.to_s+"=", value end end end And I'd make Person a Struct to get getters/setters for free.