Created
March 25, 2018 09:52
-
-
Save barsbek/25e5fd15ca2b85fcd6799bbfe6f5bae3 to your computer and use it in GitHub Desktop.
ruby: range with custom class
This file contains hidden or 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 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