Skip to content

Instantly share code, notes, and snippets.

@gunn
Created December 12, 2018 12:17
Show Gist options
  • Save gunn/a39320000dfe8c5f566012bdb0d77f45 to your computer and use it in GitHub Desktop.
Save gunn/a39320000dfe8c5f566012bdb0d77f45 to your computer and use it in GitHub Desktop.
Implement "i++" style incremention in ruby.
class NumberProxy < BasicObject
def initialize number
@number = number
end
def method_missing *args
@number.send *args
end
def inc!
@number += 1
end
def coerce other
return self, other
end
def to_ary
[@number]
end
end
class Numeric
def proxy
NumberProxy.new self
end
end
n = 5.proxy
n.inc!
thirty_six = n * n
i = 0.proxy
while i.inc! <= 10
puts i
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment