Ruby 如何分别打印我的CLI项目的名称和URL

Ruby 如何分别打印我的CLI项目的名称和URL,ruby,visual-studio-code,command-line-interface,screen-scraping,Ruby,Visual Studio Code,Command Line Interface,Screen Scraping,我在做吉他。我将吉他的名称和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

我在做吉他。我将吉他的名称和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
当我输入
.name
时,我得到:

/Users/tlreigns/RoyalTerminal/hi_strung/lib/hi_strung/cli.rb:43:in `block in menu': undefined method `name' for #<Hash:0x00007fab93b96a40> (NoMethodError)
{: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?

electrics的每个元素都是散列。您只需要值,而不是键值对

所以,我相信你只需要

puts electric[:name]


(如Amit Patel在评论中指出的那样,不使用字符串插值可以获得所需的输出。

Ahhh成功了,谢谢!还没有真正使用哈希,所以这对我来说是个新手!谢谢你!!字符串插值
“#{}”
不是必需的
put
可以打印任何类型的对象。所以,
put electric[:url]
足够打印字符串了。@AmitPatel啊,是的,你当然是对的。我只是复制和修改OP的格式,没有真正思考。谢谢
puts electric[:name]
puts electric[:url]