[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Printing variable names

darren kirby

8/18/2007 10:10:00 PM

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
--
darren kirby :: Part of the problem since 1976 :: http://badco...
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

3 Answers

Haze Noc

8/18/2007 11:00:00 PM

0

> 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?
--
Posted via http://www.ruby-....

darren kirby

8/18/2007 11:14:00 PM

0

quoth the Haze Noc:
> > 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
--
darren kirby :: Part of the problem since 1976 :: http://badco...
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

darren kirby

8/18/2007 11:36:00 PM

0

quoth the darren kirby:
> quoth the Haze Noc:
> > > 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
--
darren kirby :: Part of the problem since 1976 :: http://badco...
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972