arraysrubytypeerrorfixnum

I pass an array to a ruby method I wrote but ruby sees a fixnum?


everyone, I'm a super Ruby n00b having problems getting my first Ruby program to work. I have found Q and A here on Stack Overflow that are closely related to the problem that I am having but no matter what I try, I just can't get rid of this error.

I have written two classes, Checkout and Register. Here's the full Register class:

<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>

Here is the Register class:

<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 "Item\t\t\tPrice"
        puts "----\t\t\t-----"
        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}\t\t#{currentPrice}"
        end
        puts "-----------------------------------\n\t\t\t#{total}"
    end
end
</code>

The way these classes should work when I try to run them is as follows: I feed Checkout various items and the two classes work together to create a receipt showing what has been purchased and any discounts that could be applied whenever I call the printReceipt method.

The test that I am using to debug looks like this:

<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>

The output when I run the test looks like this:

<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>

Please help!

Thank you in advance.


Solution

  • you need to fix at 4 places,

    1. In Checkout.rb, if(ind.class == "Array") is wrong comparison

      use if(ind.class.to_s == "Array") or better use if(ind.instance_of? Array)

    2. In you main.rb, use for i in 0...basket.length instead of for i in 0..basket.length because 1..2 => [1,2] but 1...3 => [1,2]

    3. In Checkout.rb, after Class first line should be attr_accessor :register because you are access register in main.rb

    4. In main.rb, co.register.printReceipt instead of 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