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 !
Josh N
November 21, 2008, November 21, 2008 04:34, permalink
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")
Adam
November 21, 2008, November 21, 2008 05:23, permalink
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
Can this be done in one line?