Ruby 在设置这些实例变量时,我做错了什么?

Ruby 在设置这些实例变量时,我做错了什么?,ruby,instance-variables,Ruby,Instance Variables,当我调用Quote#scrape_Quote时,试图在我的Quote实例中设置或访问这些变量时,我似乎犯了一些基本错误。我可以看到值被刮取并保存到刮取的值散列中很好,当我调用quote.lifes时,我无法访问变量,例如,我得到nil 我这里出了什么错 quote.rb class Quote < ApplicationRecord require 'watir' attr_accessor :lives, :benefit, :payment def scrape_quo

当我调用
Quote#scrape_Quote
时,试图在我的
Quote
实例中设置或访问这些变量时,我似乎犯了一些基本错误。我可以看到值被刮取并保存到刮取的值散列中很好,当我调用
quote.lifes
时,我无法访问变量,例如,我得到
nil

我这里出了什么错

quote.rb

class Quote < ApplicationRecord 
  require 'watir'
  attr_accessor :lives, :benefit, :payment

  def scrape_quote
    rows = @browser.trs
    quote_rows = rows[1..8]
    scraped_values = {}

    quote_rows.each do |row|
      scraped_values[row.tds[0].text] = row.tds[1].text
    end

        @lives      = scraped_values[0]
        @benefit    = scraped_values[1]
        @payment    = scraped_values[2]

    puts scraped_values
  end
end
class Quote
刮取的值是散列而不是数组。您正在尝试访问它,就像它是一个数组一样

使用
行.tds[0]中的任何内容。text
引用哈希:

h = {a:1,b:2}
h[:a]
=> 1 

刮取的_值是散列而不是数组。您正在尝试访问它,就像它是一个数组一样

使用
行.tds[0]中的任何内容。text
引用哈希:

h = {a:1,b:2}
h[:a]
=> 1 
一般情况 如果您只想将哈希值按顺序分配给成员变量,可以使用
hash#values
返回值的并行分配,如下所示:

2.4.1 :001 > h = {}
 => {} 
2.4.1 :002 > h[:one] = 1
 => 1 
2.4.1 :003 > h[:two] = 2
 => 2 
2.4.1 :004 > h[:three] = 3
 => 3 
2.4.1 :005 > @one, @two, @three = h.values
 => [1, 2, 3] 
2.4.1 :006 > @one
 => 1 
2.4.1 :007 > @two
 => 2 
2.4.1 :008 > @three
 => 3 
2.4.1 :009 >

具体应用 您案例中的特定代码将变成:

class Quote < ApplicationRecord 
  require 'watir'
  attr_accessor :lives, :benefit, :payment

  def scrape_quote
    rows = @browser.trs
    quote_rows = rows[1..8]
    scraped_values = {}

    quote_rows.each do |row|
      scraped_values[row.tds[0].text] = row.tds[1].text
    end

    @lives, @benefit, @payment = scraped_values.values

    puts scraped_values
  end
end
class Quote
一般情况 如果您只想将哈希值按顺序分配给成员变量,可以使用
hash#values
返回值的并行分配,如下所示:

2.4.1 :001 > h = {}
 => {} 
2.4.1 :002 > h[:one] = 1
 => 1 
2.4.1 :003 > h[:two] = 2
 => 2 
2.4.1 :004 > h[:three] = 3
 => 3 
2.4.1 :005 > @one, @two, @three = h.values
 => [1, 2, 3] 
2.4.1 :006 > @one
 => 1 
2.4.1 :007 > @two
 => 2 
2.4.1 :008 > @three
 => 3 
2.4.1 :009 >

具体应用 您案例中的特定代码将变成:

class Quote < ApplicationRecord 
  require 'watir'
  attr_accessor :lives, :benefit, :payment

  def scrape_quote
    rows = @browser.trs
    quote_rows = rows[1..8]
    scraped_values = {}

    quote_rows.each do |row|
      scraped_values[row.tds[0].text] = row.tds[1].text
    end

    @lives, @benefit, @payment = scraped_values.values

    puts scraped_values
  end
end
class Quote
构建散列的惯用方法是使用
map
而不是
each
,并且不使用局部变量的预先声明

scraped_values = quote_rows.map do |row|
  [row.tds[0].text, row.tds[1].text]
end.to_h

构建散列的惯用方法是使用
map
而不是
each
,并且不使用局部变量的预先声明

scraped_values = quote_rows.map do |row|
  [row.tds[0].text, row.tds[1].text]
end.to_h

你需要的不是
scraped\u值[0]
而是这样的:
scraped\u值[scraped\u值.键[0]]
,因为scraped\u值不是数组,0,1,2和任何其他缺少的键一样,所以哈希返回
nil
而不是
scraped\u值[0]
你需要这样的东西:
scraped\u值[刮取的_值。键[0]]
,因为刮取的_值不是数组,0,1,2就像其他丢失的键一样,所以哈希返回
nil

我无法访问变量如果你尝试会发生什么?它们返回
nil
,抱歉,我会更新上面。我无法访问变量如果你尝试会发生什么?它们返回
nil
,抱歉,我会更新上面的内容。特别是键将是任何
行。tds[0]。文本将返回。您应该在打印
刮取的值时看到这些值。还需要调用
刮取引号来设置哈希值。特别是键将是任何
行。tds[0].text
返回。打印
scraped_值时应该会看到这些值
scrape_quote
也需要调用以设置哈希值。