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

#_jes_range_class_args, #_jes_range_hash

Instance Method Summary collapse

Methods included from Enumerable

#_jes_add_category, #_jes_all_categories, #_jes_average, #_jes_cartesian_product, #_jes_categories, #_jes_category_map, #_jes_category_values, #_jes_compliment, #_jes_correlation, #_jes_count_if, #_jes_covariance, #_jes_cum_max, #_jes_cum_min, #_jes_cum_prod, #_jes_cum_sum, #_jes_default_block, #_jes_default_block=, #_jes_dichotomize, #_jes_euclidian_distance, #_jes_exclusive_not, #_jes_first_category, #_jes_frequency, #_jes_frequency_for, #_jes_intersect, #_jes_is_numeric?, #_jes_max, #_jes_max_index, #_jes_max_of_lists, #_jes_median, #_jes_min, #_jes_min_index, #_jes_min_of_lists, #_jes_new_sort, #_jes_normalize, #_jes_normalize!, #_jes_order, #_jes_pearson_correlation, #_jes_product, #_jes_quantile, #_jes_rand_in_range, #_jes_range, #_jes_range_as_range, #_jes_range_class, #_jes_rank, #_jes_render_category, #_jes_scale, #_jes_scale!, #_jes_scale_between, #_jes_scale_between!, #_jes_scale_to_sigmoid, #_jes_scale_to_sigmoid!, #_jes_set_range, #_jes_set_range_class, #_jes_sigma_pairs, #_jes_standard_deviation, #_jes_sum, #_jes_tanimoto_pairs, #_jes_to_f!, #_jes_to_pairs, #_jes_union, #_jes_variance, #_jes_yield_transpose, #method_missing, safe_alias

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Enumerable

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