我目前有这个哈希,
{ "meal" => "Bomb mi ",
"meal_type " => "Bombwiches",
"times_served" => "8AM to 3PM",
"days_served" => "Tuesday - Sunday",
"notes" => "Hoisin glazed shredded pork or tofu, fresh jalapenos, house-made pickled daikon & carrots, pickles, fresh cilantro, sriracha and a fried egg. ",
"price" => "13.25" }
我可以通过@menu_items[i]
数据来自我正在使用Sinatra加载的csv
get "/menu" do
data_file = 'rabbit-sinatra.csv'
@menu_items = []
CSV.foreach(data_file, headers: true, :encoding => 'utf-8') do |row|
@menu_items << row.to_hash
end
erb :menu
end
然后我循环遍历这个并在erb中提取所有必要的值。
<% for i in 0..70 do %>
<% if @menu_items[i]["meal_type"] == "Bombwiches" %>
<div class="menu">
<div class="foo"> <%= @menu_items[i]["meal"]%> </div>
<div class="times_served"> <%= @menu_items[i]["times_served"]%> </div>
<div class="days_served"> <%= @menu_items[i]["days_served"]%> </div>
<div class="notes"> <%= @menu_items[i]["notes"]%> </div>
<div class="price"> <%= @menu_items[i]["price"]%> </div>
</div>
<% end %>
<% end %>
虽然@menu_items[i]["notes"]
和所有其他哈希解析命令都有效,但由于某些原因,当我运行
@menu_items[i]["meal"]
不返回用餐"meal" => "Bomb mi "
的值,而是返回任何内容。
不确定我错过了什么。
答案 0 :(得分:0)
无法重现。
i = 1
@menu_items[i] =
{ "meal" => "Bomb mi ",
"meal_type " => "Bombwiches",
"times_served" => "8AM to 3PM",
"days_served" => "Tuesday - Sunday",
"notes" => "Hoisin glazed shredded pork or tofu, fresh jalapenos, house-made pickled daikon & carrots, pickles, fresh cilantro, sriracha and a fried egg. ",
"price" => "13.25" }
puts @menu_items[i]["price"] #=> "13.25"
puts @menu_items[i]["meal"] #=> "Bomb mi "
显然,它并不包含您的想法。