【发布时间】:2014-10-02 05:52:05
【问题描述】:
module Framework
class CreateTableDefinition
attr_accessor :host, :username, :password
end
end
def create_table(table_name)
obj = Framework::CreateTableDefinition.new
yield(obj) if block_given?
end
create_table :users do |config|
config.host :localhost
end
这是我得到的错误
-:13:in `block in <main>': wrong number of arguments (1 for 0) (ArgumentError)
from -:9:in `create_table'
from -:12:in `<main>'
如果我将代码更改为
config.host = :localhost
它工作正常。但我想要的是按照上面的描述工作 config.host :localhost
【问题讨论】:
-
听起来您希望
CreateTableDefinition类有一个host方法,该方法将其参数分配给@host本地,听起来对吗? -
@AndrewPiliser,正确,但我想省略 = 我发现大多数 RoR 代码都会像 config.host :localhost 那样进行分配。见不 =