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?

2
  • I need to be able to take my current_location and access an array based on that value. Commented Aug 18, 2011 at 19:13
  • current_location[1] should return omaha[1] ??? Commented Aug 18, 2011 at 19:16

4 Answers 4

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]}"
Sign up to request clarification or add additional context in comments.

1 Comment

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?

6 Comments

That is very dirty and bad design as well
@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, 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.
@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.
Please don't learn such bad habits as you learn to program... eval is rarely the right tool.
|
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.

1 Comment

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.
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.

Comments

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.