如何将参数传递给AASM gem中的new / init

时间:2016-10-29 14:57:45

标签: ruby aasm

我正在尝试使用AASM gem创建一个有限状态机器链。我想检查字符串是否唯一(数据库中不存在)。

我写道:

require 'rubygems'
require 'aasm'

class Term
  include AASM

  aasm do
    state :Beginning, :initial => true
    state :CheckUniqueness

    def initialize(term)
        print term
    end

    event :UniquenessChecking do
        print "Check uniqueness"
      transitions :from => :Beginning, :to => :CheckUniqueness
    end

  end

end

term = Term.new("textstring")
term.CheckUniqueness

但是当我使用Term.new("textstring")时,它不允许我传递我认为的参数,因为我收到错误:

`initialize': wrong number of arguments (1 for 0) (ArgumentError)
 from try.rb:24:in `new'
 from try.rb:24:in `<main>'

是否可以在AASM中使用init传递参数?我想知道我该怎么做?

1 个答案:

答案 0 :(得分:1)

您在initialize块内定义了aasm,只需将其移出该块:

require 'rubygems'
require 'aasm'

class Term
  include AASM

  def initialize(term)
    print term
  end

  aasm do
    # ...
  end
end