Class: Scale::Destination::Enumerable

Inherits:
Object
  • Object
show all
Defined in:
lib/scale/destination.rb

Overview

Contains logic for dealing with input that includes Ruby’s core ::Enumerable

Instance Method Summary collapse

Constructor Details

#initialize(enum) ⇒ Enumerable

Returns a new instance of Enumerable.

Parameters:

  • enum (::Enumerable)

    An enumerable (eg Array, Set) to operate with



37
38
39
# File 'lib/scale/destination.rb', line 37

def initialize(enum)
  @enum = enum
end

Instance Method Details

#scale(input, source) ⇒ Numeric

Scale the given input and source using this destination

Parameters:

  • input (Numeric)

    A numeric value to scale

  • source (Scale::Source)

    The source for the input value

Returns:



45
46
47
48
49
# File 'lib/scale/destination.rb', line 45

def scale(input, source)
  proportion = source.numerator(input) / source.denominator
  index = [((proportion * @enum.size).to_i - 1), 0].max
  @enum.to_a.at(index)
end