Printing variable names - RUBY

This is a discussion on Printing variable names - RUBY ; 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.... ...

+ Reply to Thread
Results 1 to 4 of 4

Printing variable names

  1. Default Printing variable names

    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://badcomputer.org
    "...the number of UNIX installations has grown to 10, with more expected..."
    - Dennis Ritchie and Ken Thompson, June 1972


  2. Default Re: Printing variable names

    > 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-forum.com/.


  3. Default Re: Printing variable names

    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://badcomputer.org
    "...the number of UNIX installations has grown to 10, with more expected..."
    - Dennis Ritchie and Ken Thompson, June 1972


  4. Default Re: Printing variable names

    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://badcomputer.org
    "...the number of UNIX installations has grown to 10, with more expected..."
    - Dennis Ritchie and Ken Thompson, June 1972


+ Reply to Thread

Similar Threads

  1. funny variable names in gdb
    By Application Development in forum Fortran
    Replies: 0
    Last Post: 03-28-2007, 05:05 PM
  2. variable names for different fortrans
    By Application Development in forum Fortran
    Replies: 0
    Last Post: 03-15-2007, 12:00 PM
  3. variable names in objects
    By Application Development in forum C
    Replies: 14
    Last Post: 02-03-2004, 04:00 PM