Interpolation question with each do block

  • Thread starter Älphä Blüë
  • Start date
Ã

Älphä Blüë

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.
 
G

Gary Wright

Hi all,

I have a quick interpolation question..

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

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

datavar =3D ydspgm

rows.datavar should be equal to rows.ydspgm but it's not. I'll get =20=
an
undefined method datavar. I'm not sure how I can pass the name of =20
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 =3D #{rows.team.name} | #{datavar} =3D =20
#{rows.__send__(datavar)}"


Gary Wright=
 
Ã

Älphä Blüë

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top