0

So my goal is to be able to run through a "while" loop and in each iteration create a new variable that includes the "iteration count" within that variables name and stores it for later use outside of the loop. See below for more details.

NOTE: The code is clearly wrong in so many ways but I'm writing it this way to make it more clear? as to what I am trying to accomplish. Thanks for any input on how this is possible.

count = "4"
while count > "0"
  player"#{count}"_roll = rand(20)
  puts 'Player "#{count}" rolled: "#{player"#{count}"_roll}"'
  count -= 1
end

My goal is then to be able to access the variables that were created from within the loop at a later part of the program like so (more or less)

puts player4_roll
puts player3_roll
puts player2_roll
puts player1_roll

The key being that these variables were A) created in the loop B) With names relying on another variables input, and C) accessible outside the loop for later use.

Hopefully my question came out clear and any input will be greatly appreciated. I'm very new to programming and trying to get my head wrapped around some more complex ideas. I'm not sure if this is even possible to do in Ruby. Thanks!

4
  • is this for debugging purposes? Commented Jan 16, 2015 at 23:19
  • @ Anthony I'm so new to programming thatI'm not even sure what constitutes debugging or not, but the code is dummy code I'm making to find an answer to later be implemented in a little Texas Hold Em style game I'm making. Basically this part of the code is used in the calculating what cards a player is dealt and then creating a variable "player_x_hand" to hold the string value of the card (where x = the count in the iteration) If that all makes sense.... Commented Jan 16, 2015 at 23:23
  • 4
    I'm pretty sure this problem can be solved in another way. There are some metaprogramming ways to create variables, but I doubt it'll be useful in this case. Why don't you just store the rolls as DATA in an array instead? Commented Jan 16, 2015 at 23:28
  • @daremkd Yea I've recently started trying to find a way to use a Hash to store the card values but since this was my original idea I wanted to check if it's possible before abandoning it for another concept. Thanks for the input though :) Commented Jan 16, 2015 at 23:30

2 Answers 2

2

I think the best way is to use arrays or hashes, with arrays its something like this:

count = 0
array = []
while count < 4 do
  array[count] = rand(20)
  puts "Player #{count} rolled: #{array[count]}"
  count += 1
end

array.each do |var|
    puts var
end

You store the result in the array and then you loop trough it. If you want the result of the second iteration of the loop you do something like this:

puts array[1]

If you want to use Hashes there are some modifications you need to do:

count = 0
hash = {}
while count < 4 do
  hash["player#{count}_roll"] = rand(20)
  puts "Player #{count} rolled: #{hash["player#{count}_roll"]}"
  count += 1
end

hash.each do |key, var|
    puts var
end

If you want the result of the second iteration of the loop you do something like this:

puts hash["player1_roll"]
Sign up to request clarification or add additional context in comments.

2 Comments

I think the idea of the hash definitely works better for my purposes although putting in your code and running it returned 14: syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '(' puts hash{"player1_roll"} and 14: syntax error, unexpected $end, expecting keyword_end puts hash{"player1_roll"} ^ I think overall though if I can clear the errors, this should work great. Thanks!
Forget the error. It was my fault for copying "If you want the result of the second iteration of the loop you do something like this:" into the code. Works like a charm. Thanks again!
0

You could set the variable using instance_variable_set and reference it that way

  instance_variable_set("@player#{count}_roll", rand(20))

1 Comment

Running that puts: Player "#{count}" rolled: "@player#{count}_roll" Player "#{count}" rolled: "@player#{count}_roll" Player "#{count}" rolled: "@player#{count}_roll" Player "#{count}" rolled: "@player#{count}_roll" I'm needing: Player 4 rolled: 15 Player 3 rolled: 20 Player 2 rolled: 2 Player 1 rolled: 12 Sorry I've just never heard if instance_variable_set before and am a bit confused perhaps?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.