Skip to content

Instantly share code, notes, and snippets.

@barsbek
Created March 25, 2018 09:52
Show Gist options
  • Save barsbek/25e5fd15ca2b85fcd6799bbfe6f5bae3 to your computer and use it in GitHub Desktop.
Save barsbek/25e5fd15ca2b85fcd6799bbfe6f5bae3 to your computer and use it in GitHub Desktop.
ruby: range with custom class
class Ln
include Comparable
attr_reader :value
def initialize(n)
@current = n
@value = Math.log2(n)
end
# Objects of the range have to comparable
def <=>(another)
value <=> another.value
end
# For methods, which treat Range as sequence
# method, which gives next values has to be implemented
def succ
Ln.new(@current+1)
end
end
first = Ln.new(16)
second = Ln.new(64)
first..second # range based on Ln class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment