语法错误,意外的keyword_when,期待keyword_end(SyntaxError)

时间:2015-03-17 21:56:23

标签: ruby-on-rails ruby syntax

完成代码挑战,我必须在"注册"中添加支持。我已添加新支持的项目如下:

    require 'register'

require 'timecop'

describe Register do
  subject { described_class.new }

  describe 'Frozen Pizza' do
    it 'should be $5.00 each' do
      subject.ring_up('Frozen Pizza', quantity: 1)
      expect(subject.total).to eq 5.00
      subject.ring_up('Frozen Pizza', quantity: 1)
      expect(subject.total).to eq 10.00
    end
  end

  describe 'Corn' do
    it 'should be $0.50 each' do
      subject.ring_up('Corn', quantity: 1)
      expect(subject.total).to eq 0.50
    end
    it 'should be 5 for $2.00' do
      subject.ring_up('Corn', quantity: 5)
      expect(subject.total).to eq 2.00
      subject.ring_up('Corn', quantity: 1)
      expect(subject.total).to eq 2.50
    end
  end

  describe 'Ground Beef' do
    it 'should be $4.99/lb' do
      subject.ring_up('Ground Beef', quantity: 1)
      expect(subject.total).to eq 4.99
    end
    it 'should allow partial pounds' do
      subject.ring_up('Ground Beef', quantity: 0.5)
      expect(subject.total).to eq 2.50
    end
  end

  describe 'Chocolate Mayfield Ice Cream' do
    it 'should be $5.99' do
      subject.ring_up('Chocolate Mayfield Ice Cream', quantity: 1)
      expect(subject.total).to eq 5.99
    end
    context 'on Wednesdays' do
      before { Timecop.freeze(Time.parse('2014-06-18 13:00:00')) }
      after  { Timecop.return }

      it 'should be BOGO' do
        subject.ring_up('Chocolate Mayfield Ice Cream', quantity: 2)
        expect(subject.total).to eq 5.99
        subject.ring_up('Chocolate Mayfield Ice Cream', quantity: 1)
        expect(subject.total).to eq 11.98
      end
    end
    context 'on Non-Wednesdays' do
      before { Timecop.freeze(Time.parse('2014-06-19 13:00:00')) }
      after  { Timecop.return }

      it 'should be NOT be BOGO' do
        subject.ring_up('Chocolate Mayfield Ice Cream', quantity: 2)
        expect(subject.total).to eq 11.98
      end
    end
  end

  describe 'Vanilla Mayfield Ice Cream' do
    it 'should be $5.99' do
      subject.ring_up('Vanilla Mayfield Ice Cream', quantity: 1)
      expect(subject.total).to eq 5.99
      subject.ring_up('Vanilla Mayfield Ice Cream', quantity: 1)
      expect(subject.total).to eq 11.98
    end
  end

  describe 'Mango' do
    context 'on Mondays' do
      before { Timecop.freeze(Time.parse('2014-06-16 13:00:00')) }
      after  { Timecop.return }
      it 'should be $0.50 each' do
        subject.ring_up('Mango', quantity: 1)
        expect(subject.total).to eq 0.50
        subject.ring_up('Mango', quantity: 1)
        expect(subject.total).to eq 1.00
      end
    end
  end

    context 'on Non-Mondays' do
      before { Timecop.freeze(Time.parse('2014-06-17 13:00:00')) }
      after  { Timecop.return }
      it 'should be $1.00 each' do
        subject.ring_up('Mango', quantity: 1)
        expect(subject.total).to eq 1.00
        subject.ring_up('Mango', quantity: 1)
        expect(subject.total).to eq 2.00
      end
    end


# Adding Additional Support To the Following Items

    describe 'Mahi Fillet' do
      it 'should be $7.99' do
        subject.ring_up('Mahi Fillet', quantity: 1)
        expect(subject.total).to eq 7.99
        subject.ring_up('Mahi Fillet', quantity: 1)
        expect(subject.total).to eq 15.98
      end
    end

    describe 'Mac & Cheese' do
      it 'should be $1.29 each' do
        subject.ring_up('Mac & Cheese', quantity: 1)
        expect(subject.total).to eq 1.29
      end
        it 'should be 5 for $10.00' do
          subject.ring_up('Corn', quantity: 10)
          expect(subject.total).to eq 10.00
          subject.ring_up('Corn', quantity: 1)
          expect(subject.total).to eq 1.00
        end
    end

    describe 'Super Ripe Avacados' do
      it 'should be $1,10' do
        subject.ring_up('Super Ripe Avacados', quantity: 1)
        expect(subject.total).to eq 1.10
      end

      it 'should be BOGO' do
        subject.ring_up('Super Ripe Avacados', quantity: 3)
        expect(subject.total).to eq 1.10
        subject.ring_up('Super Ripe Avacados', quantity: 1)
        expect(subject.total).to eq 0.36666666666667
      end
    end


end

现在我正在创建一些测试,通过Rake运行并注释为#Additions,在添加一个后我得到语法错误,我完全被困在这里。

    class Register
  attr :items

  def initialize
    @items = Hash.new(0)
  end

  def ring_up(item, args)
    @items[item] += args[:quantity]
  end

  def total
    total = 0
    items.each do |item, quantity|
      case item
      when 'Frozen Pizza'
        total += quantity * 5
      when 'Corn'
        if (quantity % 5 == 0) 
          total += quantity / 5 * 2.00
        else
          bundles = quantity / 5
          singles = quantity % 5
          total += bundles * 2.00 + singles * 0.50
        end
      when 'Ground Beef'
        total += (quantity * 4.99).round(2)
      when /Mayfield Ice Cream/
        if (quantity % 2 == 0) 
          if item =~ /^Chocolate/
            if Time.now.strftime("%A") == 'Wednesday'
              total += quantity / 2 * 5.99
            else
              total += quantity * 5.99
            end
          else
            total += quantity * 5.99
          end
        else
          if item =~ /^Chocolate/
            if Time.now.strftime("%A") == 'Wednesday'
              total += (quantity / 2) * 5.99 + (quantity % 2) * 5.99
            else
              total += quantity * 5.99
            end
          else
            total += quantity * 5.99
          end
        end
      when 'Mango'
        if Time.now.strftime("%A") == 'Monday'
          total += quantity * 0.50
        else
          total += quantity * 1.00
        end
      end

#Additions

      when 'Mahi Fillet'
        total += quantity * 7.99
      end 



    end
    total
  endNow 

end

完整的错误返回如下:

syntax error, unexpected keyword_when, expecting keyword_end (SyntaxError)
      when 'Mahi Fillet'
          ^

1 个答案:

答案 0 :(得分:2)

end

  end  <<<<=====

#Additions

  when 'Mahi Fillet'

关闭整个case块,因此下一行中的when对解析器没有意义。只需删除end即可解决问题。