我有一个CLI应用程序,它将5个不同的交易页面分割并保存到@@所有类变量中。我希望他们都有一个新的变量,应该从1到100(因为总共有100个交易)。我尝试了很多,但它只显示了所有交易的第一名。遍历所有数组元素并将变量添加到元素ruby
def deals_listing
all_deals = PopularDeals::NewDeals.all
@deals = []
all_deals.collect do |deal_info|
i = 1
deal_info.number = i
@deals << deal_info
i = i + 1
end
@deals
binding.pry
end
输出的,我得到的是样品..
pry(#<PopularDeals::CLI>)> @deals
=> [#<PopularDeals::NewDeals:0x00000001aaf220
@deal_rating="+7",
@number=1,
@posted="Posted Today",
@price="$7.64",
@title=
"Back Again at Amazon Campbell's Slow Cooker Sauces, Apple Bourbon Pulled Pork, 13 Ounce (Pack of 6) as low as $7.64 w/ Subscribe and Save S&S w/ Free Shipping",
@url=
"https://slickdeals.net/f/10033448-back-again-at-amazon-campbell-s-slow-cooker-sauces-apple-bourbon-pulled-pork-13-ounce-pack-of-6-as-low-as-7-64-w-subscribe-and-save-s-
s-w-free-shipping">,
#<PopularDeals::NewDeals:0x00000001a876f8
@deal_rating="+6",
@number=1,
@posted="Posted Today",
@price="$5.33",
@title=
"LUCKLED 20 LED Solar Powered Dragonfly String Lights Multi-color $5.33 AC, FS w/prime @Amazon",
@url=
ing-lights-multi-color-5-33-ac-fs-w-prime-amazon">,
#<PopularDeals::NewDeals:0x00000001a84228
@deal_rating="+6",
@number=1,
@posted="Posted Today",
@price="$339.99",
@title=
ping @ Walmart",
@url=
efurbished-339-99-free-shipping-walmart">,
#<PopularDeals::NewDeals:0x00000001a80ad8
@deal_rating="+6",
@number=1,
:
我想吃点什么..
pry(#<PopularDeals::CLI>)> @deals
=> [#<PopularDeals::NewDeals:0x00000001aaf220
@deal_rating="+7",
@number=1,
@posted="Posted Today",
@price="$7.64",
@title=
"Back Again at Amazon Campbell's Slow Cooker Sauces, Apple Bourbon Pulled Pork, 13 Ounce (Pack of 6) as low as $7.64 w/ Subscribe and Save S&S w/ Free Shipping",
@url=
"https://slickdeals.net/f/10033448-back-again-at-amazon-campbell-s-slow-cooker-sauces-apple-bourbon-pulled-pork-13-ounce-pack-of-6-as-low-as-7-64-w-subscribe-and-save-s-
s-w-free-shipping">,
#<PopularDeals::NewDeals:0x00000001a876f8
@deal_rating="+6",
@number=2,
@posted="Posted Today",
@price="$5.33",
@title=
"LUCKLED 20 LED Solar Powered Dragonfly String Lights Multi-color $5.33 AC, FS w/prime @Amazon",
@url=
ing-lights-multi-color-5-33-ac-fs-w-prime-amazon">,
#<PopularDeals::NewDeals:0x00000001a84228
@deal_rating="+6",
@number=3,
@posted="Posted Today",
@price="$339.99",
@title=
ping @ Walmart",
@url=
efurbished-339-99-free-shipping-walmart">,
#<PopularDeals::NewDeals:0x00000001a80ad8
@deal_rating="+6",
@number=4,
:
任何建议,使其工作?提前谢谢你。
它也工作!非常感谢! –