0

Why can't I do the following:

current_location = 'omaha'
omaha = []

omaha[0] = rand(10)
omaha[1] = rand(10) + 25
omaha[2] = rand(5) + 10

puts "You are currently in #{current_location}."
puts "Fish is worth #{omaha[0]}"
puts "Coal is worth #{current_location[1]}"
puts "Cattle is worth #{current_location[2]}"

The omaha[0] line works, but the current_location[1] doesn't. I suspect it is because omaha is a string and my puts is returning an ASCII code for that letter (That is in fact what is happening).

How do I get around this?

Noah Clark
  • 8,101
  • 14
  • 74
  • 116

4 Answers4

3

Perhaps this is a better solution:

LOCDATA = Struct.new(:fish, :coal, :cattle)
location_values = Hash.new{ |hash, key| hash[key] = LOCDATA.new(rand(10), rand(10) + 25, rand(5) + 10) }

current_location = 'omaha'

puts "You are currently in #{current_location}"
puts "Fish is worth #{location_values[current_location].fish}"
puts "Coal is worth #{location_values[current_location].coal}"
puts "Cattle is worth #{location_values[current_location].cattle}"

#You may also use:
puts "Fish is worth #{location_values[current_location][0]}"
knut
  • 27,320
  • 6
  • 84
  • 112
  • This is very similar to http://stackoverflow.com/questions/7113208/how-to-store-and-retrieve-values-using-ruby/7113366#comment-8521962 – Noah Clark Aug 18 '11 at 20:40
1

You want to get this:

current_location = 'omaha'
omaha = []
omaha[0] = rand(10)
omaha[1] = rand(10) + 25
omaha[2] = rand(5) + 10
eval("#{current_location}[1]")
# the same as:
omaha[1]

Really?

fl00r
  • 82,987
  • 33
  • 217
  • 237
  • 3
    That is very dirty and bad design as well – fl00r Aug 18 '11 at 19:18
  • 1
    @NoahClark: there's a very good chance that this is not really want you want, especially if `current_location` is actually filled with user input. – Michael Kohl Aug 18 '11 at 19:22
  • 1
    @Michael, I suspect you're worried about evaluating user input and it being a security issue! This is just a simple game I'm writing as I try to learn ruby. It's something to be aware of for sure. Honestly, I think this game would be a lot better using OOP, but I'm not that far in LRTHW. – Noah Clark Aug 18 '11 at 19:26
  • 1
    @NoahClark: If you are aware of what you are doing, that's ok :-) And if you are really working through LRTHW you'll benefit a lot. I still think that if you show us exactly what you want to do — in a new question maybe — someone will have a better answer that's still easy to understand. – Michael Kohl Aug 18 '11 at 19:29
  • @Micheal http://stackoverflow.com/questions/7113208/how-to-store-and-retrieve-values-using-ruby Does this help explain what I'm trying to do? – Noah Clark Aug 18 '11 at 19:46
  • 2
    Please don't learn such bad habits as you learn to program... eval is rarely the right tool. – DGM Aug 18 '11 at 21:38
1

Which version of Ruby are you running? I've just tried this in 1.9 and it returns the letter not an ASCII reference.

Nicholas Smith
  • 11,642
  • 6
  • 37
  • 55
  • Well @fl00r resolved it but testing with RVM shows on my machine that what you were trying does return an ASCII char, it's probably been tweaked in the release. – Nicholas Smith Aug 18 '11 at 19:29
1

The simplest solution similar to your level of code so far would be to use :

locations = {}              #hash to store all locations in

locations['omaha'] = {}     #each named location contains a hash of products
locations['omaha'][:fish] = rand(10)
locations['omaha'][:coal] = rand(10) + 25
locations['omaha'][:cattle] = rand(5) + 10


puts "You are currently in #{current_location}"
puts "Fish is worth #{locations[current_location][:fish]}"
puts "Coal is worth #{locations[current_location][:coal]}"
puts "Cattle is worth #{locations[current_location][:cattle]}"

But as knut showed above, it would be better to make the products into a struct or object instead of just labels in a hash. He then went on to show how to make the default values for those products, in the statement about the hash.

DGM
  • 26,629
  • 7
  • 58
  • 79