# Printing variable names

**URL:** <https://www.ruby-forum.com/t/printing-variable-names/112441>\
**Category:** Ruby\
**Created:** [August 19, 2007, 12:12am UTC](https://www.ruby-forum.com/t/printing-variable-names/112441 "2007-08-19T00:12:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [August 19, 2007, 12:12am UTC](https://www.ruby-forum.com/t/printing-variable-names/112441/1 "2007-08-19T00:12:12Z")

</div>

Hello all,

I have an array of variables here:

foo = [s0, s1, s2, s3, s4, s5, s6, s7]

I need to loop over the array and print each variable’s name and value:

s0: 23  
s1: 56  
s2: 345345

etc…

I cannot seem to find a way to print the name. Is it possible?

Thanks for consideration,  
-d

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [August 19, 2007, 12:59am UTC](https://www.ruby-forum.com/t/printing-variable-names/112441/2 "2007-08-19T00:59:56Z")

</div>

> I cannot seem to find a way to print the name. Is it possible?
> 
> Thanks for consideration,  
> -d

Wouldn’t it be more logical to use a hash?

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [August 19, 2007, 1:16am UTC](https://www.ruby-forum.com/t/printing-variable-names/112441/3 "2007-08-19T01:16:16Z")

</div>

quoth the Haze N.:

> > I cannot seem to find a way to print the name. Is it possible?
> > 
> > Thanks for consideration,  
> > -d
> 
> Wouldn’t it be more logical to use a hash?

Perhaps, but the order of the variables is significant. I was thinking I  
might  
create a hash to map the position to the name and vice versa…

Basically what I need to do here is allow all these variables to be  
accessed  
by name and by position. The name of the vars is significant, and I need  
the  
user to be able to dump the values to see each one, hence the original  
question.

Perhaps I will think about this more and see if I cannot formulate my  
needs a  
bit better…

Thanks,  
-d

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [August 19, 2007, 1:37am UTC](https://www.ruby-forum.com/t/printing-variable-names/112441/4 "2007-08-19T01:37:17Z")

</div>

quoth the darren kirby:

> quoth the Haze N.:
> 
> > > I cannot seem to find a way to print the name. Is it possible?
> > > 
> > > Thanks for consideration,  
> > > -d
> > 
> > Wouldn’t it be more logical to use a hash?
> 
> Perhaps, but the order of the variables is significant. I was thinking I  
> might create a hash to map the position to the name and vice versa…

OK, answered my original question:

@registers = [:zero, :at, :v0, :v1, :a0, :a1, :a2, :a3,  
:t0, :t1, :t2, :t3, :t4, :t5, :t6, :t7,  
:s0, :s1, :s2, :s3, :s4, :s5, :s6, :s7,  
:t8, :t9, :k0, :k1, :gp, :sp, :fp, :ra]

@registers.each do |v|  
puts “$#{v}: #{send(v)}”  
end

…

-d
