struct Range(B, E)

Overview

A Range represents an interval: a set of values with a beginning and an end.

Ranges may be constructed using the usual new method or with literals:

x..y  # an inclusive range, in mathematics: [x, y]
x...y # an exclusive range, in mathematics: [x, y)
(x..) # an endless range, in mathematics: >= x
..y   # a beginless inclusive range, in mathematics: <= y
...y  # a beginless exclusive range, in mathematics: < y

See Range literals in the language reference.

An easy way to remember which one is inclusive and which one is exclusive it to think of the extra dot as if it pushes y further away, thus leaving it outside of the range.

Ranges typically involve integers, but can be created using arbitrary objects as long as they define succ (or pred for reverse_each), to get the next element in the range, and < and ==, to know when the range reached the end:

# Represents a string of 'x's.
struct Xs
  include Comparable(Xs)

  getter size

  def initialize(@size : Int32)
  end

  def succ
    Xs.new(@size + 1)
  end

  def <=>(other)
    @size <=> other.size
  end

  def inspect(io)
    @size.times { io << 'x' }
  end

  def to_s(io)
    io << @size << ' '
    inspect(io)
  end
end

An example of using Xs to construct a range:

r = Xs.new(3)..Xs.new(6)
r.to_s                 # => "xxx..xxxxxx"
r.to_a                 # => [Xs.new(3), Xs.new(4), Xs.new(5), Xs.new(6)]
r.includes?(Xs.new(5)) # => true

Included Modules

Defined in:

ssz/codec.cr

Class Method Summary

Instance Method Summary

Instance methods inherited from module Enumerable(B)

hash_tree_root : Bytes hash_tree_root, ssz_basic? : Bool ssz_basic?, ssz_encode(io : IO) ssz_encode, ssz_size : Int32 ssz_size, ssz_variable? : Bool ssz_variable?

Class methods inherited from module Enumerable(B)

ssz_basic? : Bool ssz_basic?

Instance methods inherited from class Object

ssz_basic? : Bool ssz_basic?, ssz_encode(io : IO)
ssz_encode : Bytes
ssz_encode
, ssz_fixed? : Bool ssz_fixed?, ssz_size : Int32 ssz_size, ssz_variable? : Bool ssz_variable?

Class methods inherited from class Object

ssz_basic? : Bool ssz_basic?, ssz_decode(io : IO, size : Int32 = 0)
ssz_decode(bytes : Bytes)
ssz_decode
, ssz_fixed? : Bool ssz_fixed?, ssz_variable? : Bool ssz_variable?

Class Method Detail

def self.ssz_basic? : Bool #

[View source]
def self.ssz_decode(io : IO, size : Int32) #

[View source]
def self.ssz_fixed? : Bool #

[View source]
def self.ssz_variable? : Bool #

[View source]

Instance Method Detail

def ssz_basic? : Bool #

[View source]
def ssz_encode(io : IO) #

[View source]
def ssz_fixed? : Bool #

[View source]
def ssz_size : Int32 #

[View source]
def ssz_variable? : Bool #

[View source]