Last active
August 29, 2015 14:09
-
-
Save keleshev/73178f616549dc0d0576 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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