7841fbf022cfe78bff34942b50c483e1

Can this be done in one line?

select = []
joins = []
%w[first second third forth].each{ |semester| joins << ":pdp_#{semester}_semester"; (1..5).each{ |n| select << "pdp_#{semester}_semesters.objetive_#{n} AS #{semester}_objetive_#{n}" }}
[select, joins].each { |x| x.join(', ') }

Refactorings

No refactoring yet !

7841fbf022cfe78bff34942b50c483e1

Ceritium

November 20, 2008, November 20, 2008 22:11, permalink

No rating. Login to rate!

testing...

60888d58de3bf08cbc0c73e67fd6fd86

Josh N

November 21, 2008, November 21, 2008 04:34, permalink

1 rating. Login to rate!

Putting it on one line while possible will be hard to read. But you can use the map() (aka collect()) function to make things simpler.

results = %w[first second third forth].map do |semester|
  (1..5).map {|n| "pdp_#{semester}_semesters.objetive_#{n} AS #{semester}_objetive_#{n}"}
end
puts results.flatten.join("\n")
A8d3f35baafdaea851914b17dae9e1fc

Adam

November 21, 2008, November 21, 2008 05:23, permalink

No rating. Login to rate!

I get the feeling you would be better off improving your schema. I'm assuming that you are using ActiveRecord. Perhaps this will help. But without more information it's hard to say what is best.

class SchoolYear < ActiveRecord::Base
  has_many :semesters
end

class Semester < ActiveRecord::Base
  belongs_to :school_year
  has_many :objectives
end

class Objective < ActiveRecord::Base
  belongs_to :semester
end

Your refactoring





Format Copy from initial code

or Cancel