Skip to content

Instantly share code, notes, and snippets.

@keleshev
Last active August 29, 2015 14:09
Show Gist options
  • Save keleshev/73178f616549dc0d0576 to your computer and use it in GitHub Desktop.
Save keleshev/73178f616549dc0d0576 to your computer and use it in GitHub Desktop.
class Value
class << self
alias [] new
end
def ==(other)
return false unless other.is_a? self.class
self.uninitialize == other.uninitialize
end
alias_method :eql?, :==
def inspect
values = uninitialize.reverse.drop_while(&:nil?).reverse
self.class.to_s + values.inspect
end
alias_method :to_s, :inspect
def hash
[self.class, uninitialize].hash
end
end
class Node < Value
attr_reader :source, :range, :children, :name
def initialize(source, range, children=nil, name=nil)
@source, @range, @children, @name = source, range, children.to_a, name
end
def uninitialize
[@source, @range, @children, @name]
end
end
p Node['source', 0..2, [], 'name']
# => Node["source", 0..2, [], "name"]
p Node['source', 0..2]
# => Node["source", 0..2, []]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment