Hi --
On Thu, 16 Jul 2009, Älphä Blüë wrote:
>
> Okay, so I figured out what I was doing wrong.
>
> This method:
>
> def calculate_tsos(model, datavar, teamvar, valvar)
> var = model.compiled_this_week.find(:all)
> var.each_with_index do |rows, i|
> teamvar[i] = rows.team.id
> valvar[i] = rows.__send__(datavar)
> puts "#{model} [ Row #{i} | Team ID = #{teamvar[i]} | Team =
> #{rows.team.name} | #{datavar} = #{valvar[i]}"
> end
> end
>
> .. works fine ..
>
> The problem was I forgot to pass empty arrays with the method variables
> in my rake file:
>
> to_team_id = []
> to_value = []
> update_tsos_offense = TsosOffense.new
> update_tsos_offense.calculate_tsos(TotalOffense, "ydspgm", to_team_id,
> to_value)
>
> However, even doing this, I would like some feedback.
>
> Should I be doing it this way or is there an easier way with what I'm
> trying to accomplish?
In general, I would tend to have the method return values that you can
then assign to local variables, rather than creating accumulator
objects and passing them in as arguments.
update_tsos_offense = TsosOffense.new
to_team_id, to_value = calculate_tsos(TotalOffense, "ydspgm")
and have the method return arrays:
def calculate_tsos(model, datavar)
teamvar, valvar = [], []
var = model.compiled_this_week.find(:all)
var.each_with_index do |rows, i|
teamvar[i] = rows.team.id
valvar[i] = rows.__send__(datavar)
puts "#{model} [ Row #{i} | Team ID = #{teamvar[i]} | Team =
#{rows.team.name} | #{datavar} = #{valvar[i]}"
end
return teamvar, valvar
end
I haven't really gotten my head into the problem-space of the
application itself, so I'm being kind of mechanistic about moving code
around (and haven't tested it), but that's at least a broad-stroke way
of rethinking it.
(Another thing to think about would be whether the calculation method
might belong in a class, rather than as a top-level routine. But I
can't really determine that from these excerpts.)
David
--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2)
Training! Intro to Ruby, with Black & Kastner, September 14-17
(More info: http://rubyurl.com/vmzN)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---