Matt
Matt

Reputation: 557

How do I use the for loop in ruby to grab different values from hash tables

This is probably easy to do! I'm not able envision the loop yet, I was thinking about a nested for loop but not quite sure how to alternate between the two hashes.

Lets say I have a class with a def that containts two hash tables:

 class Teststuff
    def test_stuff
     letters = { "0" => " A ", "1" => " B ", "2" => " C " }
     position = {"1" => "one ", "2"=> " two ", "3"=> " three ", "4"=>" four " }

     my_array=[0,1,2,2] #this represents user input stored in an array valid to 4 elements
     array_size = my_array.size #this represents the size of the user inputed array
     element_indexer = my_array.size # parellel assignment so I can use same array for array in dex
     array_start_index = element_indexer-1 #give me the ability later to get start at index zero for my array

 #for loop?? downto upto?? 
 # trying to get loop to grab the first number "0" in element position "0", grab the hash values then
 # the loop repeats and grabs the second number "1" in element position "1" grab the hash values
 # the loop repeats and grabs the third number "2" in elements position "2" grab the hash values
 # the final iteration grabs the fourth number "2" in elements position "3" grab the hash values
 # all this gets returned when called. Out put from puts statement after grabing hash values 
 # is: **A one B two C three C four**  

     return a_string
    end
  end  

How do I go about returning string output to the screen like this:

   **A one B two C three C four** 

or simply letter position letter position... Thanks for the help, put code up so I can try on my editor!

Upvotes: 0

Views: 245

Answers (3)

Jörg W Mittag
Jörg W Mittag

Reputation: 369614

I think I figured out what it is you want, although I still have no idea what array_size, element_indexer, array_start_index and TestStuff are for.

def test_stuff
  letters = { "0" => " A ", "1" => " B ", "2" => " C " }
  position = {"1" => "one ", "2"=> " two ", "3"=> " three ", "4"=>" four " }

  my_array = [0, 1, 2, 2]

  "**#{my_array.map.with_index {|e, i|
    "#{letters[e.to_s].strip} #{position[(i+1).to_s].strip}"
  }.join(' ')}**"
end

[I took the liberty of reformatting your code to standard Ruby coding style.]

However, everything would be much simpler, if there weren't all those type conversions, and all those superfluous spaces. Also, the method would be much more useful, if it actually had a way to return different results, instead of always returning the same thing, because at the moment, it is actually exactly equivalent to

def test_stuff
  '**A one B two C three C four**'
end

Something along these lines would make much more sense:

def test_stuff(*args)
  letters = %w[A B C]
  position = %w[one two three four]

  "**#{args.map.with_index {|e, i| "#{letters[e]} #{position[i]}" }.join(' ')}**"
end

test_stuff(0, 1, 2, 2)
# => '**A one B two C three C four**'

If you don't want to pollute the Object namespace with your method, you could do something like this:

def (TestStuff = Object.new).test_stuff(*args)
  letters = %w[A B C]
  position = %w[one two three four]

  "**#{args.map.with_index {|e, i| "#{letters[e]} #{position[i]}" }.join(' ')}**"
end

TestStuff.test_stuff(0, 1, 2, 2)
# => '**A one B two C three C four**'

Upvotes: 1

Zabba
Zabba

Reputation: 65517

How about :

a_string = ""
my_array.each_with_index { |x, index|
  a_string += letters[my_array[index].to_s] + " " + (position.include?((index+1).to_s) ? position[(index+1).to_s] : "nil")
}

Upvotes: 0

Victor Deryagin
Victor Deryagin

Reputation: 12225

You can use enumerators, like this:

l = letters.to_enum
p = position.to_enum
a_string = ''
loop do
  a_string << l.next[1] << p.next[1]
end

Upvotes: 1

Related Questions