Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby:访问数组_Ruby_Arrays_String - Fatal编程技术网

Ruby:访问数组

Ruby:访问数组,ruby,arrays,string,Ruby,Arrays,String,为什么我不能做到以下几点: 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]}" p

为什么我不能做到以下几点:

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]}"
omaha[0]行可以工作,但当前位置[1]无法工作。我怀疑这是因为omaha是一个字符串,而我的puts返回的是该字母的ASCII码(事实上就是这样)

我该如何解决这个问题?

您想得到这个:

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]
真的吗?

你想得到这个:

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]

真的吗?

您正在运行哪个版本的Ruby?我刚刚在1.9中尝试过,它返回的字母不是ASCII引用。

您正在运行哪个版本的Ruby?我刚刚在1.9中尝试过,它返回的字母不是ASCII引用。

也许这是一个更好的解决方案:

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]}"

也许这是一个更好的解决方案:

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]}"

到目前为止,与您的代码级别类似的最简单解决方案是使用:

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]}"

但是正如knut在上面所展示的,最好将产品制作成一个结构或对象,而不仅仅是散列中的标签。然后,他在关于散列的声明中展示了如何为这些产品生成默认值。

到目前为止,与您的代码级别类似的最简单解决方案是使用:

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]}"

但是正如knut在上面所展示的,最好将产品制作成一个结构或对象,而不仅仅是散列中的标签。然后,他在关于散列的声明中展示了如何为这些产品设置默认值。

我需要能够获取当前位置并基于该值访问数组。
current\u location[1]
应该返回
omaha[1]
???我需要能够获取我的当前位置并基于该值访问数组。
当前位置[1]
应该返回
omaha[1]
???这也是非常肮脏和糟糕的设计well@NoahClark当前位置这很有可能不是你真正想要的,尤其是当当前位置实际上充满了用户输入时。@Michael,我怀疑你担心评估用户输入,这是一个安全问题!这只是我在尝试学习ruby时编写的一个简单游戏。这是一件值得肯定的事情。老实说,我认为使用OOP这个游戏会更好,但我在LRTHW方面还不算太远。@NoahClark:如果你知道自己在做什么,那没关系:-)如果你真的通过LRTHW工作,你会受益匪浅。我仍然认为,如果你在一个新的问题中向我们展示你到底想做什么,也许有人会有一个更好的答案,这个答案仍然很容易理解。请不要像你学习编程那样学习坏习惯。。。eval很少是正确的工具。这是非常肮脏和糟糕的设计well@NoahClark:这很有可能不是您真正想要的,特别是当当前位置实际上充满了用户输入时。@Michael,我怀疑您担心评估用户输入,这是一个安全问题!这只是我在尝试学习ruby时编写的一个简单游戏。这是一件值得肯定的事情。老实说,我认为使用OOP这个游戏会更好,但我在LRTHW方面还不算太远。@NoahClark:如果你知道自己在做什么,那没关系:-)如果你真的通过LRTHW工作,你会受益匪浅。我仍然认为,如果你在一个新的问题中向我们展示你到底想做什么,也许有人会有一个更好的答案,这个答案仍然很容易理解。请不要像你学习编程那样学习坏习惯。。。eval很少是正确的工具。@fl00r解决了这个问题,但在我的机器上用RVM测试表明,您尝试的返回ASCII字符,可能在版本中进行了调整。@fl00r解决了这个问题,但用RVM测试表明,在我的机器上,您尝试的返回ASCII字符,它可能在发行版中进行了调整。这非常类似于这非常类似于