Access denied, Please sign in and make sure you have proper permission.
class Abc
def initialize(opts = {})
@title = opts[:title] || ""
@flat = opts[:flag] || ""
end
end
就是传了一个 hash
class ABC
def initialize params
p params
end
end
这个不叫用 symbol 做参数,是个 hash 做参数,只是省略了大括号。
#1 楼 @huacnlee
class Abc
def initialize title: '', flag: ''
@title = title
@flag = flag
end
end
不是有这么个经典写法么
class Abc
attr_accessor :title, :flag
def initialize(opts = {})
opts.each do |opt, value|
send("#{opt=}", value)
end
end
end