Class: FixedRange

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/fixed_range.rb

Overview

Because the standard Range isn’t robust enough to handle floating point ranges correctly.

Instance Attribute Summary collapse

Attributes included from Enumerable

#range_class_args

Instance Method Summary collapse

Methods included from Enumerable

#average, #cartesian_product, #categories, #compliment, #correlation, #cum_max, #cum_min, #cum_prod, #cum_sum, #default_block, #default_block=, #euclidian_distance, #exclusive_not, #intersect, #is_numeric?, #max_index, #max_of_lists, #median, #min_index, #min_of_lists, #new_sort, #order, #original_max, #original_min, #product, #quantile, #rand_in_range, #range, #range_as_range, #range_class, #rank, #set_range_class, #sigma_pairs, #standard_deviation, #sum, #tanimoto_pairs, #to_pairs, #union, #variance, #yield_transpose

Constructor Details

#initialize(min, max, step_size = 1) ⇒ FixedRange

Returns a new instance of FixedRange.



7
8
9
10
11
12
13
14
15
16
# File 'lib/fixed_range.rb', line 7

def initialize(min, max, step_size=1)
  @step_size = step_size
  if (min <=> max) < 0
    @min = min
    @max = max
  else
    @min = max
    @max = min
  end
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



6
7
8
# File 'lib/fixed_range.rb', line 6

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



6
7
8
# File 'lib/fixed_range.rb', line 6

def min
  @min
end

#step_sizeObject (readonly)

Returns the value of attribute step_size.



6
7
8
# File 'lib/fixed_range.rb', line 6

def step_size
  @step_size
end

Instance Method Details

#each(&block) ⇒ Object



28
29
30
# File 'lib/fixed_range.rb', line 28

def each(&block)
  step(&block)
end

#sizeObject



18
19
20
# File 'lib/fixed_range.rb', line 18

def size
  @size ||= calc_size
end

#step(enn = self.step_size, &block) ⇒ Object



22
23
24
25
26
# File 'lib/fixed_range.rb', line 22

def step(enn=self.step_size, &block)
  calc_size(enn).to_i.times do |i|
    block.call(step_value(i, enn))
  end
end

#step_value(index, step_size = self.step_size) ⇒ Object Also known as: []

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
# File 'lib/fixed_range.rb', line 32

def step_value(index, step_size=self.step_size)
  index = size.to_i + index if index < 0
  val = (index * step_size) + self.min
  raise ArgumentError, "You have supplied an index and/or step_size that is outside of the range" if
    val < self.min or val > self.max
  return val
end