class Series

Constants

VERSION

Attributes

expression[RW]
lower_bound[RW]
upper_bound[RW]

Public Class Methods

new(lower_bound: 1, upper_bound: Float::INFINITY, &expression) click to toggle source
# File lib/series.rb, line 8
def initialize(lower_bound: 1, upper_bound: Float::INFINITY, &expression)
  @expression = expression
  @enumerator = expression_to_enum
  @lower_bound = lower_bound
  @upper_bound = upper_bound
end

Public Instance Methods

[](index) click to toggle source
# File lib/series.rb, line 15
def [](index)
  expression.call(index)
end
each() { |n| ... } click to toggle source
# File lib/series.rb, line 19
def each
  return to_enum(__callee__) unless block_given?
  @enumerator.each { |n| yield n }
end
partial_sum(bound) click to toggle source
# File lib/series.rb, line 24
def partial_sum(bound)
  take(bound).reduce(:+)
end

Private Instance Methods

expression_to_enum() { |self| ... } click to toggle source
# File lib/series.rb, line 31
def expression_to_enum
  return to_enum(__callee__) unless block_given?
  (lower_bound..upper_bound).each { |n| yield self[n] }
end