From: "Marcel Molina Jr." Date: 2005-11-02T11:24:16+09:00 Subject: Re: How can I avoid this boring code? On Wed, Nov 02, 2005 at 11:17:08AM +0900, Sam Kong wrote: > 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? You could use Active Record. class Person < ActiveRecord::Base end >> some_person = Person.new >> some_person.last_name = 'Kong' >> some_person.save etc Though ActiveRecord is known for being a component of Rails, it can be used entirely discreetly from Rails. e.g. require 'active_record' ActiveRecord::Base.establish_connection(:adapter => 'sqlite', :database => ':memory:') class Person < Active::Record::Base end All set. marcel -- Marcel Molina Jr.