【发布时间】:2015-06-01 08:38:37
【问题描述】:
是否可以使用新的 ruby 2.0 语法访问包含关键字参数的哈希值?
class List::Node
attr_accessor :data, :next
def initialize data: nil, next: nil
self.data = data
self.next = next # ERROR!
end
end
旧语法可以正常工作:
class List::Node
attr_accessor :data, :next
def initialize options = { data: nil, next: nil }
self.data = options[:data]
self.next = options[:next]
end
end
----- 编辑-----
我意识到next 是一个保留字,但我猜测关键字属性在内部存储在哈希中,我想知道是否可以访问它,例如通过self.args、self.parameters、self.options等
【问题讨论】: