Skip to content

Instantly share code, notes, and snippets.

@bastosmichael
Last active August 29, 2015 14:01
Show Gist options
  • Save bastosmichael/a701f8ada44b19315a45 to your computer and use it in GitHub Desktop.
Save bastosmichael/a701f8ada44b19315a45 to your computer and use it in GitHub Desktop.
mass object assignment without an array or enumerable
class Xs # represent a string of 'x's
include Comparable
attr :length
def initialize(n)
@length = n
end
def succ
Xs.new(@length + 1)
end
def <=>(other)
@length <=> other.length
end
def to_s
sprintf "%2d #{inspect}", @length
end
def inspect
'x' * @length
end
end
r = Xs.new(3)..Xs.new(6) #=> xxx..xxxxxx
r.to_a #=> [xxx, xxxx, xxxxx, xxxxxx]
r.member?(Xs.new(5)) #=> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment