Class: Tonal::Log
- Inherits:
-
Object
- Object
- Tonal::Log
- Extended by:
- Forwardable
- Includes:
- Comparable
- Defined in:
- lib/tonal/log.rb
Direct Known Subclasses
Constant Summary collapse
- PRECISION =
2
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#logarithm ⇒ Object
readonly
Returns the value of attribute logarithm.
-
#logarithmand ⇒ Object
readonly
Returns the value of attribute logarithmand.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(rhs) ⇒ Object
- #initialize(logarithmand: nil, logarithm: nil, base: nil) ⇒ Tonal::Log constructor
-
#inspect ⇒ String
The string representation of Tonal::Log.
-
#step(modulo) ⇒ Tonal::Step
The nearest step in the given modulo.
-
#to_cents(precision: Tonal::Cents::PRECISION) ⇒ Tonal::Cents
The cents scale logarithm.
Constructor Details
#initialize(logarithmand: nil, logarithm: nil, base: nil) ⇒ Tonal::Log
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/tonal/log.rb', line 18 def initialize(logarithmand: nil, logarithm: nil, base: nil) raise ArgumentError, "logarithmand or logarithm must be provided" if logarithmand.nil? && logarithm.nil? raise ArgumentError, "logarithmand must be Numeric" unless logarithmand.kind_of?(Numeric) || logarithmand.nil? raise ArgumentError, "logarithm must be Numeric" unless logarithm.kind_of?(Numeric) || logarithm.nil? if logarithmand && logarithm && base @logarithmand = logarithmand @logarithm = logarithm @base = derive_base(logarithmand: logarithmand, logarithm: logarithm) puts "Provided base (#{base}) does not align with logarithmand and logarithm. Using calculated base (#{@base}) instead" if @base != base elsif logarithmand && logarithm @logarithmand = logarithmand @logarithm = logarithm @base = derive_base(logarithmand: logarithmand, logarithm: logarithm) elsif logarithmand && base @logarithmand = logarithmand @base = base @logarithm = derive_logarithm(logarithmand: logarithmand, base: @base) elsif logarithm && base @base = base @logarithm = logarithm @logarithmand = derive_logarithmand(logarithm: logarithm, base: @base) elsif logarithmand @logarithmand = logarithmand @base = self.class.base @logarithm = derive_logarithm(logarithmand: logarithmand, base: @base) elsif logarithm @base = self.class.base @logarithm = logarithm @logarithmand = derive_logarithmand(logarithm: logarithm, base: @base) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(op, *args, &blk) ⇒ Object (private)
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/tonal/log.rb', line 104 def method_missing(op, *args, &blk) rhs = args.collect do |arg| arg.kind_of?(self.class) ? arg.inspect : arg end result = logarithm.send(op, *rhs) return result if op == :coerce case result when Numeric self.class.new(result) else result end end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
9 10 11 |
# File 'lib/tonal/log.rb', line 9 def base @base end |
#logarithm ⇒ Object (readonly)
Returns the value of attribute logarithm.
9 10 11 |
# File 'lib/tonal/log.rb', line 9 def logarithm @logarithm end |
#logarithmand ⇒ Object (readonly)
Returns the value of attribute logarithmand.
9 10 11 |
# File 'lib/tonal/log.rb', line 9 def logarithmand @logarithmand end |
Class Method Details
.base ⇒ Object
51 52 53 |
# File 'lib/tonal/log.rb', line 51 def self.base Math::E end |
Instance Method Details
#<=>(rhs) ⇒ Object
80 81 82 |
# File 'lib/tonal/log.rb', line 80 def <=>(rhs) rhs.kind_of?(self.class) ? logarithm <=> rhs.logarithm : logarithm <=> rhs end |
#inspect ⇒ String
Returns the string representation of Tonal::Log.
76 77 78 |
# File 'lib/tonal/log.rb', line 76 def inspect "#{logarithm.round(PRECISION)}" end |
#step(modulo) ⇒ Tonal::Step
Returns the nearest step in the given modulo.
68 69 70 |
# File 'lib/tonal/log.rb', line 68 def step(modulo) Tonal::Step.new(modulo: modulo, log: self) end |