Ruby keyword as named hash parameter -
is possible access hash holding keyword arguments using new ruby 2.0 syntax?
class list::node attr_accessor :data, :next def initialize data: nil, next: nil self.data = data self.next = next # error! end end old syntax works fine:
class list::node attr_accessor :data, :next def initialize options = { data: nil, next: nil } self.data = options[:data] self.next = options[:next] end end ----- edit -----
i realize next reserved word i'm guessing keyword attributes stored internally in hash , i'm wondering whether it's possible access it, e.g. via self.args, self.parameters, self.options, etc.
this work:
class list::node attr_accessor :data, :next def initialize data: nil, _next: nil self.data = data self.next = _next end end next ruby reserved word. use names not ruby's reserved keyword.
edit: yes, possible, not idea.
class list::node attr_accessor :data, :next def initialize data: nil, next: nil self.data = data self.next = binding.local_variable_get(:next) end end p list::node.new.next # nil look local_variable_get .
Comments
Post a Comment