带散列键的ruby-access数组

带散列键的ruby-access数组,ruby,Ruby,我正在努力理解如何使用哈希键访问数组。在我的代码中,我创建了一个包含键和值的散列。现在,我想在汽车类中设置值。每当我尝试实例化Car时,参数都需要整数而不是字符串 我得到以下错误:TypeError没有将字符串隐式转换为整数 这是我的密码: class Car_maker attr_accessor :car_maker def initialize(car_maker) @car_maker = car_maker end end class Car_model < C

我正在努力理解如何使用哈希键访问数组。在我的代码中,我创建了一个包含键和值的散列。现在,我想在汽车类中设置值。每当我尝试实例化Car时,参数都需要整数而不是字符串

我得到以下错误:TypeError没有将字符串隐式转换为整数

这是我的密码:

class Car_maker

 attr_accessor :car_maker

 def initialize(car_maker)
  @car_maker = car_maker
 end

end

class Car_model < Car_maker

attr_accessor :km, :type, :transmission, :stock, :drivetrain, :status, 
:fuel, :car_maker, :model, :year, :trim, :features
#total number of instances & array with car objects
@@totalCars = 0
@@catalogue = []

def initialize(km, type, transmission, stock, drivetrain, status, fuel, car_maker, model, year, trim, features)
    super(car_maker)
    @km = km
    @type = type
    @transmission = transmission
    @stock = stock
    @drivetrain = drivetrain
    @status = status
    @fuel = fuel
    @model = model
    @year = year
    @trim = trim
    @features = features
    @@totalCars += 1
end 

def self.convertListings2Catalogue(line)
    #Initialise arrays and use them to compare
    type = ["Sedan", "coupe", "hatchback", "station", "SUV"]
    transmission = ["auto", "manual", "steptronic"]
    drivetrain = ["FWD", "RWD", "AWD"]
    status = ["new", "used"]
    car_maker = ["honda", "toyota", "mercedes", "bmw", "lexus"]
    hash = Hash.new

    #In this part, we hash the set of features using regex
    copyOfLine = line
    regex = Regexp.new(/{(.*?)}/)
    match_array = copyOfLine.scan(regex)

    match_array.each do |line|
        hash["features"] = line
    end

    #Now, we split every comma and start matching fields
    newStr = line[0...line.index('{')] + line[line.index('}')+1...line.length]
    arrayOfElements = newStr.split(',')

    arrayOfElements.each do |value|
        if value.include?("km") and !value.include?("/")
            hash["km"] = value
        elsif type.include?(value)
            hash["type"] = value
        elsif transmission.include?(value.downcase)
            hash["transmission"] = value
        elsif value.include?("/") and value.include?("km")
            hash["fuel economy"] = value
        elsif drivetrain.include?(value)
            hash["drivetrain"] = value
        elsif status.include?(value.downcase)
            hash["status"] = value
        elsif /(?=.*[a-zA-Z])(?=.*[0-9])/.match(value) and !value.include?("km")
            hash["stock"] = value
        elsif car_maker.include?(value.downcase)
            hash["carmaker"] = value
        elsif /^\d{4}$/.match(value)
            hash["year"] = value
        elsif value.length == 2
            hash["trim"] = value
        else
            if value.length > 2
                hash["model"] = value
            end
        end 
    end 
  end
 end

 textFile = File.open('cars.txt', 'r')
 textFile.each_line{|line|
   if line.length > 2
    result = Car_model.convertListings2Catalogue(line)
    puts "Hash: #{result}"

    carObj = Car_model.new(result["km"], result["type"], result["transmission"], result["stock"], result["drivetrain"],
    result["status"], result["fuel"], result["carmaker"], result["model"], result["year"], result["trim"], result["features"])
    #@@catalogue.push (carObj)
end
}
这条线

result = Car_model.convertListings2Catalogue(line)
不返回哈希对象。它返回ArrayOffElements,因为这是each方法实际返回的内容,并且each方法是该方法中执行的最后一个方法。尽管其中有哈希赋值,但除非使用显式返回语句,否则它只返回最后一个值

只需在ConvertingListing2Catalog方法的最后一行使用变量哈希


仔细想想,这个方法中创建了几个变量。没有理由期望任何特定变量(如哈希)的内容会被返回,ruby方法在默认情况下会返回最后执行的命令。

您试图使用哈希键访问的数组是什么?关键是什么?在最后几行中,我试图通过调用car_model创建一个car。newresult[type]……type是关键,但它需要一个整数,因为result是一个数组。我如何解决这个问题?对不起,但是,结果的内容是什么?例如。预期输入,预期输出。结果是一个包含以下内容的散列表:{功能=>[AC,加热座椅,加热后视镜,免钥匙进入],公里=>65101km,类型=>轿车,变速器=>手动,库存=>18131A,传动系=>FWD,状态=>已使用,燃油经济性=>5.5L/100km,汽车制造商=>丰田,车型=>凯美瑞,内饰=>SE,年份=>2010}
        if value.length > 2
          hash["model"] = value
        end
      end 
    end 
    hash # < this is the last line of the method so it's the value that will be returned
  end
end