Interpolation question with each do block

Hi all,

I have a quick interpolation question…

def calculate_tsos(model, datavar)
var = model.find(:all)
var.each do |rows|
puts “#{model} (Team = #{rows.team.name} | #{datavar} =
#{rows.datavar}”
end
end

The only thing that doesn’t work is the datavar within interpolation in
the puts statement. How do I make it so that my datavar value is passed
to datavar in the puts statement so that it would be similar to:

datavar = ydspgm

rows.datavar should be equal to rows.ydspgm but it’s not. I’ll get an
undefined method datavar. I’m not sure how I can pass the name of that
variable to that interpolation piece of code.

thanks.

On Jul 16, 2009, at 1:50 PM, Älphä Blüë wrote:

end
an
undefined method datavar. I’m not sure how I can pass the name of
that
variable to that interpolation piece of code.

I’m not 100% sure but I believe, the problem is that you want to
call the method named by the contents of datavar but instead you
are calling the method named ‘datavar’, which of course isn’t defined.

Try this instead:

puts “#{model} (Team = #{rows.team.name} | #{datavar} =
#{rows.send(datavar)}”

Gary W.

Thanks a ton Gary - that did the trick. I’m still getting used to how
variables work in Ruby and passing variables in interpolation. I wasn’t
aware of send so I’ll read up on that.

Many thanks and much appreciated.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs