我将阵列传递给我写的红宝石方法,但Ruby看到了fixnum



大家,我是一个超级红宝石n00b,遇到了我的第一个Ruby程序工作的问题。我在堆栈溢出上找到了Q和A,这与我遇到的问题密切相关,但无论我尝试什么,我都无法摆脱此错误。

我写了两个课程,结帐和注册。这是完整的注册类:

<code>
load 'Register.rb'
class Checkout
    def initialize
        @register = Register.new
        @itemCount = Hash['CH1', 0, 'AP1', 0, 'CF1', 0, 'MK1', 0]
        @@inventory = Hash['CH1', 3.11, 'AP1', 6.00, 'CF1', 11.23, 'MK1', 4.75]
        @@discount = Hash['CF1', ['BOGO', '@itemCount["CF1"]%2==0', -11.23, '@register.findLast("CF1")'], 'AP1', ['APPL', '@itemCount["AP1"]>=3', -1.50, '@itemCount["AP1"]==3 ? @register.findAll("AP1") : @register.findLast("AP1")'], 'MK1', ['CHMK', '@itemCount["MK1"]==1 && @itemCount["CH1"]==1', -4.75, '@register.findAll("MK1")']]   
    end
    def scan(item)
        #get price of item from inventory
        price = @@inventory[item]
        #add item and price to register
        @register.ringUp(item, price)
        @itemCount[item]+=1
        #find and apply any applicable special
        discountCheck = @@discount[item]
        unless discountCheck == nil
            nameOfDiscount = @@discount[item][0]
            discountCondition = @@discount[item][1]
            moneyOff = @@discount[item][2]
            howToGetItemIndex = @@discount[item][3]
            if(eval(discountCondition))
                ind = eval(howToGetItemIndex)
                if(ind.class == "Array")
                    @register.applyDiscount(ind, moneyOff, nameOfDiscount)
                else #it's a Fixnum so we want to put it in an array first
                    indArray = [ind]
                    @register.applyDiscount(indArray, moneyOff, nameOfDiscount)    
                end
            end
        end
    end
end
</code>

这是注册类:

<code>
class Register
    def initialize
        @itemsInOrderOfScan = Array.new
        @itemInfoInOrderOfScan = Array.new
    end
    def ringUp(item, price)
        @itemsInOrderOfScan.push(item)
        @itemInfoInOrderOfScan.push(['', price])
    end
    def applyDiscount(indices, moneyOff, nameOfDiscount)
        for i in 0..indices.length-1
            ind = indices[i]
            newInd = ind + 1
            @itemsInOrderOfScan.insert(newInd, '')
            @itemInfoInOrderOfScan.insert(newInd, [nameOfDiscount, moneyOff])
        end
    end
    def findLast(item)
        arr = Array.new
        ind = @itemsInOrderOfScan.rindex(item)
        arr.push(ind)
        arr
    end
    def findAll(item)
        indexOfFirstInstanceOfItem = @itemsInOrderOfScan.index(item)
        arr = findLast(item)
        indexOfLastInstanceOfItem = arr.at(0)
        for i in indexOfFirstInstanceOfItem..indexOfLastInstanceOfItem
            if(@itemsInOrderOfScan.at(i) == item)
                arr.push(i)          
            end
        end
        arr
    end
    def printReceipt
        puts "ItemtttPrice"
        puts "----ttt-----"
        total = 0
        for i in 0..@itemsInOrderOfScan.length-1
            currentItem = @itemsInOrderOfScan.at(i)
            currentDiscountName = @itemInfoInOrderOfScan.at(i)[0]
            currentPrice = @itemInfoInOrderOfScan.at(i)[1]
            total += currentPrice
            puts "#{currentItem}t#{currentDiscountName}tt#{currentPrice}"
        end
        puts "-----------------------------------nttt#{total}"
    end
end
</code>

当我尝试运行它们时,这些类应正常工作的方式如下:我喂食各种项目,两个类共同创建收据,显示已购买的东西以及每当我称呼的任何折扣printreceipt方法。

我用来调试的测试看起来像这样:

<code>
load 'Checkout.rb'
basket = ['CH1', 'AP1', 'AP1', 'AP1', 'MK1']
co = Checkout.new
for i in 0..basket.length
    puts basket.at(i)
    co.scan(basket[i])
end
co.register.print()
</code>

我运行测试时的输出如下:

<code>
    CH1
    AP1
    AP1
    AP1
    Register.rb:16:in `+': no implicit conversion of Fixnum into Array (TypeError)
            from Register.rb:16:in `block in applyDiscount'
            from Register.rb:14:in `each'
            from Register.rb:14:in `applyDiscount'
            from Checkout.rb:33:in `scan'
            from main.rb:9:in `block in <main>'
            from main.rb:7:in `each'
            from main.rb:7:in `<main>'
</code>

请帮助!

预先感谢您。

您需要在4个位置修复

  1. Checkout.rb中,if(ind.class == "Array")是错误的比较

    使用if(ind.class.to_s == "Array")或更好地使用if(ind.instance_of? Array)

  2. 在您的main.rb中,使用for i in 0...basket.length代替for i in 0..basket.length,因为1..2 => [1,2]但是1...3 => [1,2]

  3. Checkout.rb中,一行之后应为attr_accessor :register,因为您是main.rb

  4. 中的访问登记册
  5. main.rb中,co.register.printReceipt而不是co.register.print()

main.rb:

load 'Checkout.rb'
basket = ['CH1', 'AP1', 'AP1', 'AP1', 'MK1']
co = Checkout.new
for i in 0...basket.length
    puts basket.at(i)
    co.scan(basket[i])
end
co.register.printReceipt

Checkout.rb:

load 'Register.rb'
class Checkout
    attr_accessor :register
    def initialize
        @register = Register.new
        @itemCount = Hash['CH1', 0, 'AP1', 0, 'CF1', 0, 'MK1', 0]
        @@inventory = Hash['CH1', 3.11, 'AP1', 6.00, 'CF1', 11.23, 'MK1', 4.75]
        @@discount = Hash['CF1', ['BOGO', '@itemCount["CF1"]%2==0', -11.23, '@register.findLast("CF1")'], 'AP1', ['APPL', '@itemCount["AP1"]>=3', -1.50, '@itemCount["AP1"]==3 ? @register.findAll("AP1") : @register.findLast("AP1")'], 'MK1', ['CHMK', '@itemCount["MK1"]==1 && @itemCount["CH1"]==1', -4.75, '@register.findAll("MK1")']]
    end
    def scan(item)
        #get price of item from inventory
        price = @@inventory[item]
        #add item and price to register
        @register.ringUp(item, price)
        p item
        p @itemCount[item]
        @itemCount[item]+=1
        #find and apply any applicable special
        discountCheck = @@discount[item]
        unless discountCheck == nil
            nameOfDiscount = @@discount[item][0]
            discountCondition = @@discount[item][1]
            moneyOff = @@discount[item][2]
            howToGetItemIndex = @@discount[item][3]
            if(eval(discountCondition))
                ind = eval(howToGetItemIndex)
                if(ind.class.to_s == "Array")
                    @register.applyDiscount(ind, moneyOff, nameOfDiscount)
                else #it's a Fixnum so we want to put it in an array first
                    indArray = [ind]
                    @register.applyDiscount(indArray, moneyOff, nameOfDiscount)
                end
            end
        end
    end
end

最新更新