Skip to content

Commit

Permalink
Explore in tst/ranges.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettraux committed Mar 11, 2024
1 parent ca79526 commit 6f0bbb7
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tst/ranges.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

#
# exploring ranges...

# 0-59
# 10~30

class R
include Comparable

MAX = 59
attr_reader :current, :type, :slash

def initialize(current, type, slash)
@current = current
@type = type
@slash = slash
end

def to_i
@current
end

def <=>(b)
current <=> b.to_i
end
def succ
c = current
loop do
c = c.succ
return R.new(c, type, slash) if (c % slash) == 0
fail 'overflow' if c > MAX
end
end
end

p (R.new(0, :hyphen, 1)..R.new(59, :hyphen, 1)).to_a.size

range = R.new(0, :hyphen, 2)..R.new(59, :hyphen, 2)
p range.to_a.size

p range.include?(0)
p range.include?(1)
p range.include?(2)
p range.include?(100)

p range.cover?(R.new(10, :hyphen, 2)..R.new(20, :hyphen, 2))
p range.cover?(R.new(10, :hyphen, 2)..R.new(70, :hyphen, 2))
p range.cover?(R.new(10, :hyphen, 1)..R.new(20, :hyphen, 1))

p range.to_a.collect(&:to_i)

0 comments on commit 6f0bbb7

Please sign in to comment.