# Interpolation question with each do block

**URL:** <https://www.ruby-forum.com/t/interpolation-question-with-each-do-block/172207>\
**Category:** Ruby\
**Created:** [July 16, 2009, 7:50pm UTC](https://www.ruby-forum.com/t/interpolation-question-with-each-do-block/172207 "2009-07-16T19:50:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 16, 2009, 7:50pm UTC](https://www.ruby-forum.com/t/interpolation-question-with-each-do-block/172207/1 "2009-07-16T19:50:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 16, 2009, 8:31pm UTC](https://www.ruby-forum.com/t/interpolation-question-with-each-do-block/172207/2 "2009-07-16T20:31:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 16, 2009, 8:49pm UTC](https://www.ruby-forum.com/t/interpolation-question-with-each-do-block/172207/3 "2009-07-16T20:49:08Z")

</div>

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.
