Ruby 我无法识别的错误

Ruby 我无法识别的错误,ruby,Ruby,我之前运行过这段代码: require "awesome_print" require "rexml/document" require "debugger" include REXML class Scrapper attr_reader :data def initialize file = File.new("./cia-1996.xml") @data = REXML::Document.new(file) end def get_country_

我之前运行过这段代码:

require "awesome_print"
require "rexml/document"
require "debugger"

include REXML

class Scrapper
  attr_reader :data

  def initialize
    file = File.new("./cia-1996.xml")
    @data = REXML::Document.new(file)
  end

  def get_country_inflation
    inflation_hash = {}
    XPath.match( data, "//country").map { |element| 
      inflation_hash[element.attributes["name"]] = element.attributes["inflation"].to_i}
      nested_array = inflation_hash.to_a
      sorted_array = nested_array.sort_by {|country, inflation_value| inflation_value}.reverse
      puts "The countries with the highest inflation indexes in 1996 were:"
      first_five = sorted_array.first(5)
      first_five.each do |item|
        puts "#{item[0]}, with an inflation index of #{item[1]}"
      end
    end
  end
end


sample = Scrapper.new
sample.get_country_inflation
在做了一些编辑之后,我现在得到了错误消息

economics_challenge.rb:36:语法错误,意外的关键字\u结束,预期输入结束

你能给我指点一下错误/打字错误可能在哪里吗(我已经盯着它看了一段时间了,希望能有一双新眼睛的反馈)

非常感谢你

编辑: 因此,我进行了建议的更改,但收到了更多错误消息:

economics_challenge.rb:26: syntax error, unexpected tSTRING_DEND, expecting keyword_end
economics_challenge.rb:29: syntax error, unexpected tSTRING_DEND, expecting '}'
...flation_value| inflation_value}.reverse
...                               ^
economics_challenge.rb:35: syntax error, unexpected keyword_end, expecting '}'
economics_challenge.rb:46: syntax error, unexpected end-of-input, expecting '}'
第26行是指下面代码段中的第2行:代码段(我认为这就是最初的问题所在):

第29行是:

sorted_array = nested_array.sort_by {|country, inflation_value| inflation_value}.reverse
我将尝试通过对排序数组调用reverse并将其保存到一个变量来修复29中的错误

第35行是结束语句,没有第46行

有什么建议吗

谢谢大家!

第二次编辑: 哇!我不敢相信我没有意识到我没有结束很多事情。从现在起,我将坚持使用
do和
end语法


谢谢你们两位帮了我这么多…真的很感激

从这里开始,您的缩进被弄乱了:

XPath.match( data, "//country").map { |element| 
  inflation_hash[element.attributes["name"]] = element.attributes["inflation"].to_i}
  nested_array = inflation_hash.to_a
该摘录的最后一行应该不包含一个级别,因为传递给“map”的块在第二行右括号处终止

试着把它修好

此外,这里还有一个技巧:始终使用
do
end
编写多行块,并将
end
放在自己的行上。那么你可能会:

XPath.match( data, "//country").map do |element| 
  inflation_hash[element.attributes["name"]] = element.attributes["inflation"].to_i
end
nested_array = inflation_hash.to_a

从这里开始,您的缩进被弄乱了:

XPath.match( data, "//country").map { |element| 
  inflation_hash[element.attributes["name"]] = element.attributes["inflation"].to_i}
  nested_array = inflation_hash.to_a
该摘录的最后一行应该不包含一个级别,因为传递给“map”的块在第二行右括号处终止

试着把它修好

此外,这里还有一个技巧:始终使用
do
end
编写多行块,并将
end
放在自己的行上。那么你可能会:

XPath.match( data, "//country").map do |element| 
  inflation_hash[element.attributes["name"]] = element.attributes["inflation"].to_i
end
nested_array = inflation_hash.to_a

从这里开始,您的缩进被弄乱了:

XPath.match( data, "//country").map { |element| 
  inflation_hash[element.attributes["name"]] = element.attributes["inflation"].to_i}
  nested_array = inflation_hash.to_a
该摘录的最后一行应该不包含一个级别,因为传递给“map”的块在第二行右括号处终止

试着把它修好

此外,这里还有一个技巧:始终使用
do
end
编写多行块,并将
end
放在自己的行上。那么你可能会:

XPath.match( data, "//country").map do |element| 
  inflation_hash[element.attributes["name"]] = element.attributes["inflation"].to_i
end
nested_array = inflation_hash.to_a

从这里开始,您的缩进被弄乱了:

XPath.match( data, "//country").map { |element| 
  inflation_hash[element.attributes["name"]] = element.attributes["inflation"].to_i}
  nested_array = inflation_hash.to_a
该摘录的最后一行应该不包含一个级别,因为传递给“map”的块在第二行右括号处终止

试着把它修好

此外,这里还有一个技巧:始终使用
do
end
编写多行块,并将
end
放在自己的行上。那么你可能会:

XPath.match( data, "//country").map do |element| 
  inflation_hash[element.attributes["name"]] = element.attributes["inflation"].to_i
end
nested_array = inflation_hash.to_a

问题是您有一个额外的
end

如前所述,如果您的缩进是正确的,您可能已经发现了它

当然,这是您在这里介绍的代码。虽然错误消息与找到的问题相匹配,但可能并非如此

以下是带有缩进、一些样式更改和无语法错误的代码:

require 'awesome_print'
require 'rexml/document'
require 'debugger'

include REXML

class Scrapper
  attr_reader :data

  def initialize
    file = File.new('./cia-1996.xml')
    @data = REXML::Document.new(file)
  end

  def get_country_inflation
    inflation_hash = {}
    XPath.match(data, '//country').map do |element|
      inflation_hash[element.attributes['name']] = element.attributes['inflation'].to_i
    end
    nested_array = inflation_hash.to_a
    sorted_array = nested_array.sort_by do |country, inflation_value|
      inflation_value
    end.reverse
    puts 'The countries with the highest inflation indexes in 1996 were:'
    first_five = sorted_array.first(5)
    first_five.each do |item|
      puts "#{item[0]}, with an inflation index of #{item[1]}"
    end
  end
end

sample = Scrapper.new
sample.get_country_inflation

问题是您有一个额外的
end

如前所述,如果您的缩进是正确的,您可能已经发现了它

当然,这是您在这里介绍的代码。虽然错误消息与找到的问题相匹配,但可能并非如此

以下是带有缩进、一些样式更改和无语法错误的代码:

require 'awesome_print'
require 'rexml/document'
require 'debugger'

include REXML

class Scrapper
  attr_reader :data

  def initialize
    file = File.new('./cia-1996.xml')
    @data = REXML::Document.new(file)
  end

  def get_country_inflation
    inflation_hash = {}
    XPath.match(data, '//country').map do |element|
      inflation_hash[element.attributes['name']] = element.attributes['inflation'].to_i
    end
    nested_array = inflation_hash.to_a
    sorted_array = nested_array.sort_by do |country, inflation_value|
      inflation_value
    end.reverse
    puts 'The countries with the highest inflation indexes in 1996 were:'
    first_five = sorted_array.first(5)
    first_five.each do |item|
      puts "#{item[0]}, with an inflation index of #{item[1]}"
    end
  end
end

sample = Scrapper.new
sample.get_country_inflation

问题是您有一个额外的
end

如前所述,如果您的缩进是正确的,您可能已经发现了它

当然,这是您在这里介绍的代码。虽然错误消息与找到的问题相匹配,但可能并非如此

以下是带有缩进、一些样式更改和无语法错误的代码:

require 'awesome_print'
require 'rexml/document'
require 'debugger'

include REXML

class Scrapper
  attr_reader :data

  def initialize
    file = File.new('./cia-1996.xml')
    @data = REXML::Document.new(file)
  end

  def get_country_inflation
    inflation_hash = {}
    XPath.match(data, '//country').map do |element|
      inflation_hash[element.attributes['name']] = element.attributes['inflation'].to_i
    end
    nested_array = inflation_hash.to_a
    sorted_array = nested_array.sort_by do |country, inflation_value|
      inflation_value
    end.reverse
    puts 'The countries with the highest inflation indexes in 1996 were:'
    first_five = sorted_array.first(5)
    first_five.each do |item|
      puts "#{item[0]}, with an inflation index of #{item[1]}"
    end
  end
end

sample = Scrapper.new
sample.get_country_inflation

问题是您有一个额外的
end

如前所述,如果您的缩进是正确的,您可能已经发现了它

当然,这是您在这里介绍的代码。虽然错误消息与找到的问题相匹配,但可能并非如此

以下是带有缩进、一些样式更改和无语法错误的代码:

require 'awesome_print'
require 'rexml/document'
require 'debugger'

include REXML

class Scrapper
  attr_reader :data

  def initialize
    file = File.new('./cia-1996.xml')
    @data = REXML::Document.new(file)
  end

  def get_country_inflation
    inflation_hash = {}
    XPath.match(data, '//country').map do |element|
      inflation_hash[element.attributes['name']] = element.attributes['inflation'].to_i
    end
    nested_array = inflation_hash.to_a
    sorted_array = nested_array.sort_by do |country, inflation_value|
      inflation_value
    end.reverse
    puts 'The countries with the highest inflation indexes in 1996 were:'
    first_five = sorted_array.first(5)
    first_five.each do |item|
      puts "#{item[0]}, with an inflation index of #{item[1]}"
    end
  end
end

sample = Scrapper.new
sample.get_country_inflation

Ruby告诉您第36行出现语法错误。你能确定哪一行是第36行吗?我只数了你发布的代码中的32行。第36行是最后一个结束语句。我注释掉了文档中的一些行,因此代码由36行改为32行。谢谢你帮助我!Ruby告诉您第36行出现语法错误。你能确定哪一行是第36行吗?我只数了你发布的代码中的32行。第36行是最后一个结束语句。我注释掉了文档中的一些行,因此代码由36行改为32行。谢谢你帮助我!Ruby告诉您第36行出现语法错误。你能确定哪一行是第36行吗?我只数了你发布的代码中的32行。第36行是最后一个结束语句。我注释掉了文档中的一些行,因此代码由36行改为32行。谢谢你帮助我!Ruby告诉您第36行出现语法错误。你能确定哪一行是第36行吗?我只数了你发布的代码中的32行。第36行是最后一个结束语句。我注释掉了文档中的一些行,因此代码由36行改为32行。谢谢你帮助我!非常感谢你!将立即进行编辑,看看会发生什么!附言:我喜欢你的小费;我的导师也敦促我们这样做。我一直在逃避更多的打字,但现在我将遵循你们的提示(尤其是因为它将有助于处理此类错误)。再次感谢:)非常感谢!将立即进行编辑,看看会发生什么!附言:我喜欢你的小费;我的导师也敦促我们这样做。我本来打算不再打字了,但威尔