Я создаю CLI для просмотра гитар. Я поместил имя и URL-адрес гитары в массив хэшей:
def self.get_electric
doc = Nokogiri::HTML(open("https://reverb.com/c/electric-guitars"))
electrics = []
counter = 0
while counter < doc.css("h2:contains('Popular Electric Guitars')+div.overflowing-row_items ul.tiles.tiles--single-row.tiles--grow.tiles--three-wide li.tiles_tile").length
electric = {
name: doc.css("h2:contains('Popular Electric Guitars')+div.overflowing-row_items ul.tiles.tiles--single-row.tiles--grow.tiles--three-wide li.tiles_tile")[counter].text,
url: doc.css("h2:contains('Popular Electric Guitars')+div.overflowing-row_items ul.tiles.tiles--single-row.tiles--grow.tiles--three-wide li.tiles_tile a.marketing-callout__inner")[counter]["href"]
}
counter += 1
electrics << electric
end
electrics
end
def self.get_acoustic
doc = Nokogiri::HTML(open("https://reverb.com/c/acoustic-guitars"))
acoustics = []
counter = 0
while counter < doc.css("h2:contains('Popular in Acoustic Guitars')+div.overflowing-row_items ul.tiles.tiles--single-row.tiles--grow.tiles--three-wide li.tiles_tile").length
acoustic = {
name: doc.css("h2:contains('Popular in Acoustic Guitars')+div.overflowing-row_items ul.tiles.tiles--single-row.tiles--grow.tiles--three-wide li.tiles_tile")[counter].text,
url: doc.css("h2:contains('Popular in Acoustic Guitars')+div.overflowing-row_items ul.tiles.tiles--single-row.tiles--grow.tiles--three-wide li.tiles_tile a.marketing-callout__inner")[counter]["href"]
}
counter += 1
acoustics << acoustic
end
acoustics
end
В моем классе CLI, когда пользователь вводит слова «электрический» или «акустический», ему возвращаются имя и URL-адрес. Вот один из методов:
while (input = gets.strip.downcase) != 'exit'
case input
when "electric"
electrics = HiStrung::Scraper.get_electric
electrics.each do |electric|
puts "#{electric.name}"
end
Когда я ставлю .name
, я получаю:
/Users/tlreigns/RoyalTerminal/hi_strung/lib/hi_strung/cli.rb:43:in `block in menu': undefined method `name' for #<Hash:0x00007fab93b96a40> (NoMethodError)
, но если я просто делаю #{electric}
, я получаю:
{:name=>"Fender Telecaster", :url=>"https://reverb.com/marketplace/electric-guitars?query=telecaster"}
{:name=>"Gibson Les Paul", :url=>"https://reverb.com/marketplace?query=les%20paul"}
{:name=>"Fender Stratocaster", :url=>"https://reverb.com/marketplace/electric-guitars?query=stratocaster"}
{:name=>"Gibson SG", :url=>"https://reverb.com/marketplace?query=sg"}
{:name=>"Fender Jazzmaster", :url=>"https://reverb.com/marketplace?query=jazzmaster"}
Как получить имя и URL отдельно?