麻烦在#Array和#Enumerable类中混合到我的自定义类

时间:2015-04-12 22:58:48

标签: ruby class mixins

我正在尝试将#Array和#Enumerable类混合到我的自定义类中,以便我可以使用push,pop,size和inject方法(以及其他一些方法)。我尝试使用include Array以及class RPNCalculator < Array,我认为这是正确的语法,但得到了错误:

home/action/12_rpn_calculator.rb:2:in `include': wrong argument type Class (expected Module) (TypeError)

所以我尝试使用module Array,它似乎正在运行,但没有抓住方法。所以我真的很困惑。这是我的代码:

class RPNCalculator 
  include Array 

  attr_accessor :calculator, :result


  def initialize
    @calculator = []
    @result = 0
  end

  #def push(*num)
    #@calculator.push(*num)
  #end

  def has_enough #check if there are enough args
    if @calculator.count < 2
      raise "calculator is empty"
      false
    else
      true
    end
  end

  def result  
  end

  def get_nums(source) #get as many numbers as needed from array for method
    calculate = []
    source.map{|n| calculate << n.pop}
    calculate
  end

  def plus
    if has_enough then get_nums(@calculator).inject(:+) end  
  end

  def minus
  end

  def divide
  end

  def times
  end

  end

为什么我无法使用include Arrayclass Myclass < Array运行它?

如果它有帮助,这是rspec:

require_relative "12_rpn_calculator"


    describe RPNCalculator do

  attr_accessor :calculator

  before do
    @calculator = RPNCalculator.new
  end

  it "adds two numbers" do
    calculator.push(2)
    calculator.push(3)
    calculator.plus
    calculator.value.should == 5
  end

  it "adds three numbers" do
    calculator.push(2)
    calculator.push(3)
    calculator.push(4)
    calculator.plus
    calculator.value.should == 7
    calculator.plus
    calculator.value.should == 9
  end

  it "subtracts the second number from the first number" do
    calculator.push(2)
    calculator.push(3)
    calculator.minus
    calculator.value.should == -1
  end

  it "adds and subtracts" do
    calculator.push(2)
    calculator.push(3)
    calculator.push(4)
    calculator.minus
    calculator.value.should == -1
    calculator.plus
    calculator.value.should == 1
  end

  it "multiplies and divides" do
    calculator.push(2)
    calculator.push(3)
    calculator.push(4)
    calculator.divide
    calculator.value.should == (3.0 / 4.0)
    calculator.times
    calculator.value.should == 2.0 * (3.0 / 4.0)
  end

  it "resolves operator precedence unambiguously" do
    # 1 2 + 3 * => (1 + 2) * 3
    calculator.push(1)
    calculator.push(2)
    calculator.plus
    calculator.push(3)
    calculator.times
    calculator.value.should == (1+2)*3

    @calculator = RPNCalculator.new
    # 1 2 3 * + => 1 + (2 * 3)
    calculator.push(1)
    calculator.push(2)
    calculator.push(3)
    calculator.times
    calculator.plus
    calculator.value.should == 1+(2*3)
  end

  it "fails informatively when there's not enough values stacked away" do
    expect {
      calculator.plus
    }.to raise_error("calculator is empty")

    expect {
      calculator.minus
    }.to raise_error("calculator is empty")

    expect {
      calculator.times
    }.to raise_error("calculator is empty")

    expect {
      calculator.divide
    }.to raise_error("calculator is empty")
  end

  # extra credit
  it "tokenizes a string" do
    calculator.tokens("1 2 3 * + 4 5 - /").should ==
      [1, 2, 3, :*, :+, 4, 5, :-, :/]
  end

  # extra credit
  it "evaluates a string" do
    calculator.evaluate("1 2 3 * +").should ==
      ((2 * 3) + 1)

    calculator.evaluate("4 5 -").should ==
      (4 - 5)

    calculator.evaluate("2 3 /").should ==
      (2.0 / 3.0)

    calculator.evaluate("1 2 3 * + 4 5 - /").should ==
      (1.0 + (2 * 3)) / (4 - 5)
  end

end

1 个答案:

答案 0 :(得分:0)

只需编写自定义pop / push方法,因为如果处理的数据类型是Array类型,那么pop和push将可用。

Ruby: extract features of a Stack from the Array class