Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Interpolation question with each do block

Reply
Thread Tools

Interpolation question with each do block

 
 
Älphä Blüë
Guest
Posts: n/a
 
      07-16-2009
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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Gary Wright
Guest
Posts: n/a
 
      07-16-2009

On Jul 16, 2009, at 1:50 PM, =C4lph=E4 Bl=FC=EB wrote:

> 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=

 
Reply With Quote
 
 
 
 
Älphä Blüë
Guest
Posts: n/a
 
      07-16-2009
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.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Transform a 2D color image into 2 images of (R1,G1,B) at each pixelof image 1 and (R2,G2,B) at each pixel of image2 for STEREO visualization 88888 Dihedral C++ 10 12-23-2011 02:28 PM
Fo:Block can you check to see if a block contains any text by using the block id? morrell XML 1 10-10-2006 07:18 PM
how do i? Full scan of each control in each grid row cell John Blair ASP .Net 1 08-03-2005 11:02 AM
xsl:for-each for each 3 elements problem Tjerk Wolterink XML 3 11-03-2004 05:22 PM



Advertisments