尝试理解调用哈希值的不同值的语法。 例如,假设我试图删除“裤子”如何设置这样的参数:
products = {124 => ['shoes', 59.99], 352 => ['shirt', 19.99], 777 => ['pants', 19.87],
667 => ['jacket', 39.99], 898 => ['shoulder_holster', 22.78]}
在为这个哈希编写菜单驱动的程序时,我在删除或添加密钥之前包括错误检查,这是我到目前为止所做的:
if a == 3 # Loop delete a Product
puts "Delete a Product"
d = gets.to_s # Get value for argument
while products.has_value?( d + syntax for right here???? )!= true do
puts "This turned out false because product does not exsist!"
d = gets.to_s
end
puts "Congrats your out of the loop"
products.delete(d + again syntax problems ???? )
puts products
端
如果我要删除裤子,如何输入参数的语法。它是([d,:数字])我没有运气在线任何有关如何删除或添加此方案的资源。任何帮助或代码示例将不胜感激,
马特
答案 0 :(得分:2)
products.to_a.select {|a| a.last.first == 'pants' }
这将为您提供与'裤子'匹配的记录。
[[777, ["pants", 19.87]]]
所以我认为你会想要
while !products.to_a.select {|a| a.last.first == d }.empty?
在你的循环上然后使用Dafydd的行来删除记录。
答案 1 :(得分:1)
编写“从哈希中删除命名产品”方法
这样做的方法较短,但为了清晰起见我想出了这个:
products = {124 => ['shoes', 59.99], 352 => ['shirt', 19.99], 777 => ['pants', 19.87],
667 => ['jacket', 39.99], 898 => ['shoulder_holster', 22.78]}
def wipeProduct(hash, nameToDelete)
hash.each do |i|
key = i[0]
productName = i[1].first
hash.delete(key) if productName==nameToDelete
end
end
puts products.inspect
wipeProduct(products,'pants')
puts products.inspect
wipeProduct(products,'shoulder_holster')
puts products.inspect
bash-3.2$ ruby prod.rb
{352=>["shirt", 19.99], 898=>["shoulder_holster", 22.78], 667=>["jacket", 39.99], 777=>["pants", 19.87], 124=>["shoes", 59.99]}
{352=>["shirt", 19.99], 898=>["shoulder_holster", 22.78], 667=>["jacket", 39.99], 124=>["shoes", 59.99]}
{352=>["shirt", 19.99], 667=>["jacket", 39.99], 124=>["shoes", 59.99]}
我不知道是否有可能在多个地方的哈希中出现“裤子”,但由于我使用了“hash.each(...)”,方法wipeProduct(hash,nameToDelete)将测试每一个哈希条目。
输入类型错误及其解决方法
当你接受输入时,你将你捕获的字符串分配给d。这是证据:
irb(main):010:0> d = gets.to_s
12
=> "12\n"
irb(main):011:0> d.class
=> String
您可以将该字符串转换为Fixnum,如下所示:
irb(main):012:0> d.to_i
=> 12
irb(main):013:0> d.to_i.class
=> Fixnum
产品哈希中的所有键都是Fixnums。这是证据:
irb(main):014:0> products.keys.each {|i| puts i.class}
Fixnum
Fixnum
Fixnum
Fixnum
Fixnum
=> [352, 898, 667, 777, 124]
所以你需要用这一行捕获参数的值:
d = gets.to_i # Get value for argument
答案的删除部分:
从产品中,您可以使用以下方式以编程方式删除裤子条目:
products.delete(777)
运行它可以解决这个问题:
irb(main):003:0> products.delete(777)
=> ["pants", 19.87]
请注意,您将键值(在本例中为777)提供给.delete(),并且它将分别返回一个由该键中的键和值组成的数组。
替代实施
我不确定修改散列在散列中的键值对的块中是否安全。如果不是,您可以保存所有要删除的密钥,并在迭代哈希后删除它们:
def wipeProduct(hash, nameToDelete)
keysToDelete = []
hash.each do |i|
key = i[0]
productName = i[1].first
keysToDelete << key if productName==nameToDelete
end
keysToDelete.each {|key| hash.delete(key) }
end
答案 2 :(得分:1)
这是删除“裤子”条目的更简洁的方法:
def wipeProduct(hash, nameToDelete)
hash.reject!{|key,value| nameToDelete==value.first}
end
拒绝! block获取查看每个键值对,当它返回true时,提供的键值将从哈希值中删除。
答案 3 :(得分:1)
取决于用户是输入ID号还是名称“裤子”。如果是前者:
if a == 3 # Loop delete a Product
puts "Delete a Product"
d = gets # Get value for argument
until products.has_key?(d.to_i)
puts "This turned out false because product does not exsist!"
d = gets
end
puts "Congrats your out of the loop"
products.delete(d.to_i)
puts products
end
如果它是“裤子”,那么这就是你想要的方式:
if a == 3 # Loop delete a Product
puts "Delete a Product"
d = gets.strip # Need to strip because otherwise the newline will wreck it
until products.find {|key, val| val.first == d}
puts "This turned out false because product does not exsist!"
d = gets.strip
end
puts "Congrats your out of the loop"
products.delete_if {|key, val| val.first == d}
puts products
end
答案 4 :(得分:0)
if a == 3 # Loop delete a Product
puts "Delete a Product by its key number"
d = gets
while products.has_key?(d)!= false do
puts "You have selected a key that is not currently in use"
d = gets
end
puts "You have deleted"
products.delete(d)
puts products
end
这是我最后做的事情,直到循环因为换了一个while循环而有些麻烦,因为它不会因某种原因接受新输入的键