Interpolation question with each do block

Discussion in 'Ruby' started by Älphä Blüë, Jul 16, 2009.

  1. 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/.
    Älphä Blüë, Jul 16, 2009
    #1
    1. Advertising

  2. Älphä Blüë

    Gary Wright Guest

    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=
    Gary Wright, Jul 16, 2009
    #2
    1. Advertising

  3. 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/.
    Älphä Blüë, Jul 16, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Blair
    Replies:
    1
    Views:
    396
    Eliyahu Goldin
    Aug 3, 2005
  2. Tjerk Wolterink

    xsl:for-each for each 3 elements problem

    Tjerk Wolterink, Nov 3, 2004, in forum: XML
    Replies:
    3
    Views:
    416
    Tjerk Wolterink
    Nov 3, 2004
  3. morrell
    Replies:
    1
    Views:
    933
    roy axenov
    Oct 10, 2006
  4. Pat Maddox
    Replies:
    6
    Views:
    141
    Marcin Mielżyński
    Jan 20, 2006
  5. Igor Nn
    Replies:
    7
    Views:
    426
    Johnny Morrice
    May 28, 2011
Loading...

Share This Page