Class: Tonal::Cents
- Inherits:
-
Object
- Object
- Tonal::Cents
- Extended by:
- Forwardable
- Includes:
- Comparable
- Defined in:
- lib/tonal/cents.rb
Constant Summary collapse
- HUNDREDTHS_ROUNDOFF =
-2
- FLOAT_PRECISION =
100
- CENT_SCALE =
1200.0
- TOLERANCE =
5
- PRECISION =
2
Instance Attribute Summary collapse
-
#log ⇒ Object
readonly
Returns the value of attribute log.
-
#ratio ⇒ Object
readonly
Returns the value of attribute ratio.
Class Method Summary collapse
-
.default_tolerance ⇒ Tonal::Cents
The default cents tolerance.
Instance Method Summary collapse
-
#<=>(rhs) ⇒ Object
Challenges to comparing floats www.rubydoc.info/gems/rubocop/RuboCop/Cop/Lint/FloatComparison embeddeduse.com/2019/08/26/qt-compare-two-floats/.
- #initialize(cents: nil, log: nil, ratio: nil, precision: PRECISION) ⇒ Tonal::Cents constructor
-
#inspect ⇒ Object
(also: #to_s)
- String
-
the string representation of Tonal::Cents.
-
#nearest_hundredth ⇒ Object
- Tonal::Cents
-
nearest hundredth cent value.
-
#nearest_hundredth_difference ⇒ Object
- Tonal::Cents
-
nearest hundredth cent difference.
-
#plus_minus(limit = 5) ⇒ Array
A tuple of self offset positively/negatively by limit.
-
#value(precision: @precision) ⇒ Float
(also: #cents, #to_f)
Value of self.
Constructor Details
#initialize(cents: nil, log: nil, ratio: nil, precision: PRECISION) ⇒ Tonal::Cents
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/tonal/cents.rb', line 23 def initialize(cents: nil, log: nil, ratio: nil, precision: PRECISION) raise ArgumentError, "One of cents:, log: or ratio: must be provided" unless [cents, log, ratio].compact.count == 1 @precision = precision if cents @log = derive_log(cents: cents) @value = derive_cents(cents: cents) @ratio = derive_ratio(log: @log) elsif log @log = derive_log(log: log) @value = derive_cents(log: @log) @ratio = derive_ratio(log: @log) elsif ratio @log = derive_log(ratio: ratio) @value = derive_cents(log: @log) @ratio = derive_ratio(ratio: ratio) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(op, *args, &blk) ⇒ Object (private)
All these operators are binary except for |, ~ and typeof , so the left hand (A) object would be the context object, and the right hand object (B) would be the argument passed to the operator member in A. For unary operators, there won’t be arguments, and the function would be called upon its target. operator functions would only be called if the operator is explicitly used in the source code.
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/tonal/cents.rb', line 131 def method_missing(op, *args, &blk) rhs = args.collect do |arg| arg.kind_of?(self.class) ? arg.value : arg end result = value.send(op, *rhs) return result if op == :coerce case result when Numeric self.class.new(cents: result) else result end end |
Instance Attribute Details
#log ⇒ Object (readonly)
Returns the value of attribute log.
13 14 15 |
# File 'lib/tonal/cents.rb', line 13 def log @log end |
#ratio ⇒ Object (readonly)
Returns the value of attribute ratio.
13 14 15 |
# File 'lib/tonal/cents.rb', line 13 def ratio @ratio end |
Class Method Details
.default_tolerance ⇒ Tonal::Cents
Returns the default cents tolerance.
47 48 49 |
# File 'lib/tonal/cents.rb', line 47 def self.default_tolerance self.new(cents: TOLERANCE) end |
Instance Method Details
#<=>(rhs) ⇒ Object
Challenges to comparing floats www.rubydoc.info/gems/rubocop/RuboCop/Cop/Lint/FloatComparison embeddeduse.com/2019/08/26/qt-compare-two-floats/
103 104 105 |
# File 'lib/tonal/cents.rb', line 103 def <=>(rhs) rhs.kind_of?(self.class) ? value.round(PRECISION) <=> rhs.value.round(PRECISION) : value.round(PRECISION) <=> rhs.round(PRECISION) end |
#inspect ⇒ Object Also known as: to_s
Returns [String] the string representation of Tonal::Cents.
93 94 95 |
# File 'lib/tonal/cents.rb', line 93 def inspect "#{value.round(@precision)}" end |
#nearest_hundredth ⇒ Object
Returns [Tonal::Cents] nearest hundredth cent value.
66 67 68 |
# File 'lib/tonal/cents.rb', line 66 def nearest_hundredth self.class.new(cents: value.round(Tonal::Cents::HUNDREDTHS_ROUNDOFF).to_f) end |
#nearest_hundredth_difference ⇒ Object
Returns [Tonal::Cents] nearest hundredth cent difference.
75 76 77 |
# File 'lib/tonal/cents.rb', line 75 def nearest_hundredth_difference self.class.new(cents: (value - nearest_hundredth)) end |
#plus_minus(limit = 5) ⇒ Array
Returns a tuple of self offset positively/negatively by limit.
84 85 86 |
# File 'lib/tonal/cents.rb', line 84 def plus_minus(limit = 5) [self - limit, self + limit] end |
#value(precision: @precision) ⇒ Float Also known as: cents, to_f
Returns value of self.
55 56 57 |
# File 'lib/tonal/cents.rb', line 55 def value(precision: @precision) @value.round(precision) end |