ruby中未定义的方法和False类错误

时间:2017-08-23 13:26:00

标签: ruby

我正在尝试在ruby上制作餐馆管理代码。但它在getOrder方法中向我显示错误。

控制台中显示的错误消息在下面给出

"main.rb:26:in `getOrder': undefined method `>' for false:FalseClass (NoMethodError)                                                                                             
    from main.rb:45:in `<main>' "

我的代码如下:

#creating a class
class Hotel

  # creating a local haah variable with the name of the items and amount

  detailedMenu ={"Pizza"=>100, "FriedChicken"=>70, "Burrito" => 50}

  #creating class (array) variable to store item and amount seperately
  @@menus= detailedMenu.keys
  @@amount= detailedMenu.values
  puts  @@amount.at(1) 

  #initializatin method
  def initialize(item,number)
    @item=item
    @amount= number

  end


  def getOrder()
    #checking wheather the item is in item list or not
    if @@menus.include?(@item)
        a=@@menus.index(@item)
        $remainingAmmount= @@amount.at(a)

        #checking wheather the required amount of the item is available or not
        if @amount<= $remainingAmmount & @amount> 0
            puts " Your order has been placed successfuly "
            $remainingAmmount -=@amount
            #@@amount.at(a)= $remainingAmmount
        elsif @amount<=0
            puts "Please enter a valid amount"    
        else
            puts "Sorry, we don't have that much #@item . You can try for a little less amount"
        end

    else
        puts "We don't serve this item. Please choose from #@@menus"
    end
  end
end

#creating new object
order1 = Hotel.new("Pizza" , 50)
#calling the method
order1.getOrder

1 个答案:

答案 0 :(得分:1)

使用 for (Simple s: simple.getSimpleList()) { Simple simple = getSimpleById(s, id); if (simple != null) return simple; } ,而不是&&

&