我正在尝试使用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传递参数?我想知道我该怎么做?
答案 0 :(得分:1)
您在initialize
块内定义了aasm
,只需将其移出该块:
require 'rubygems'
require 'aasm'
class Term
include AASM
def initialize(term)
print term
end
aasm do
# ...
end
end