Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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_Conditional Statements_Hash - Fatal编程技术网

Ruby 从哈希数组中搜索和提取结果

Ruby 从哈希数组中搜索和提取结果,ruby,arrays,conditional-statements,hash,Ruby,Arrays,Conditional Statements,Hash,我正在做一个练习,我有两个杂货店物品的散列数组。我在提示用户“你在找什么食物?”我试图获取用户的答案,并确定: 如果-他们的响应是散列数组#1,那么做点什么 elsif-他们的响应是以散列数组形式出现的#2,做点什么 否则-做点什么 我不能让它工作。无论我输入什么样的用户输入,我都会从第一个if语句中得到响应,“我在购物清单上找到了您的商品。” 以下是散列、数组和代码(不起作用): 谢谢。贾斯汀,是的,将每个替换为任何?可以解决问题,但是您的代码还有一些其他问题需要了解 首先,您的两种方法在功能

我正在做一个练习,我有两个杂货店物品的散列数组。我在提示用户“你在找什么食物?”我试图获取用户的答案,并确定:

如果-他们的响应是散列数组#1,那么做点什么
elsif-他们的响应是以散列数组形式出现的#2,做点什么
否则-做点什么

我不能让它工作。无论我输入什么样的用户输入,我都会从第一个if语句中得到响应,“我在购物清单上找到了您的商品。”

以下是散列、数组和代码(不起作用):


谢谢。

贾斯汀,是的,将
每个
替换为
任何?
可以解决问题,但是您的代码还有一些其他问题需要了解

首先,您的两种方法在功能上是相同的。是的,您为方法和块变量指定了不同的名称,但它们的作用是相同的。您可以通过两次调用其中一个来确认,一次是传递参数
杂货店列表
,一次是传递参数
杂货店购物车

其次,您可以通过将
@choice
的值作为参数传递,而不是将其硬连接,从而使您的方法
是否在\u杂货店\u列表中?
可能更通用。所以考虑这样写:

milk    = {:item => "milk",    :aisle => 15, :price => 3.29}
grapes  = {:item => "grapes",  :aisle => 1,  :price => 7.99}
eggs    = {:item => "eggs",    :aisle => 12, :price => 1.95}  
peanuts = {:item => "peanuts", :aisle => 17, :price => 5.98} 

grocery_list = [milk, grapes, eggs]
grocery_cart = [peanuts]

def item_present?(arr, item)
  arr.any? do |food|
    food[:item] == item
  end
end

puts "Type an item from your Grocery List to find out what aisle it is on: "
print "> "
choice = gets.chomp.downcase.strip

if item_present?(grocery_list, choice)
  puts "I found your item on the grocery list."
elsif item_present?(grocery_cart, choice)
  puts "Your item is already in your cart."
else puts "Your item is not on either list."
end 
假设用户要输入“鸡蛋”。上面写着“我在杂货店的清单上找到了你的东西”,这很好,但是假设你也想告诉那个人鸡蛋在过道上。一种方法是将
项目\u present?
更改为:

def item_present?(arr, item)
  arr.any? do |food|
    (food[:item] == item) ? food[:aisle] : false
  end
end
也就是说,它不会返回
true
false
,而是返回过道(这是一个真实值)或
false
。这是相当混乱的,但是,也许可以考虑使用不同的数据结构。这里有一种可能性:

food = { "milk"    => { :loc => :list, :aisle => 15, :price => 3.29},
         "grapes"  => { :loc => :list, :aisle => 1,  :price => 7.99},
         "eggs"    => { :loc => :list, :aisle => 12, :price => 1.95},
         "peanuts" => { :loc => :cart, :aisle => 17, :price => 5.98}
       } 

def item_present?(food, type, item)
  food.any? { |f| food.key?(item) && food[item][:loc] == type }
end

puts "Type an item from your Grocery List to find out what aisle it is on: "
print "> "
choice = gets.chomp.downcase.strip

if item_present?(food, :list, choice)
  puts "I found your item on the grocery list. Try aisle #{food[choice][:aisle]}"
elsif item_present?(food, :cart, choice)
  puts "Your item is already in your cart.  Are you blind?"
else puts "Your item is not on your list or in your cart."
end
现在,如果用户输入“鸡蛋”,将打印以下内容:

"I found your item on the grocery list. Try aisle 12"

我建议对这样的事情使用
方法而不是
每个
。你的问题是什么?使用
any?
方法有效。谢谢@JustinWood!好主意。感谢您提供的所有信息。谢谢@CarySwoveland!
"I found your item on the grocery list. Try aisle 12"