[Ruby] refactoring/drying help

Harry Dean Hudson Jr. dean at ero.com
Thu Oct 11 15:47:23 PDT 2007


On 10/11/07, shane becker <veganstraightedge at gmail.com> wrote:

[...]

> i'm walking through and saving them out as individual records in my
> new people table. the way i'm doing it is obviously not DRY. i
> couldn't figure out to do it any other way.
>
> sooo... how could i do this more dry?

You probably want to initialize that array outside the loop for
starters... then just pull the common bits out of the if  blocks:

people =[]

Artist.find(:all).each do |artist|
  person = Person.new
  person.full_name = artist.first_name_last_name
  person.first_name = person.full_name.split.first
  person.last_name = person.full_name.split.last

  if artist.writer then
    person.role_id = 1
  elsif artist.artist then
    person.role_id = 2
  end

  people << person if person.save
end


More information about the Ruby mailing list