Class: Tonal::Log
- Inherits:
-
Object
- Object
- Tonal::Log
- Extended by:
- Forwardable
- Includes:
- Comparable
- Defined in:
- lib/tonal/log.rb
Direct Known Subclasses
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
16 17 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 |
# File 'lib/tonal/log.rb', line 16 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)
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/tonal/log.rb', line 102 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.
7 8 9 |
# File 'lib/tonal/log.rb', line 7 def base @base end |
#logarithm ⇒ Object (readonly)
Returns the value of attribute logarithm.
7 8 9 |
# File 'lib/tonal/log.rb', line 7 def logarithm @logarithm end |
#logarithmand ⇒ Object (readonly)
Returns the value of attribute logarithmand.
7 8 9 |
# File 'lib/tonal/log.rb', line 7 def logarithmand @logarithmand end |
Class Method Details
.base ⇒ Object
49 50 51 |
# File 'lib/tonal/log.rb', line 49 def self.base Math::E end |
Instance Method Details
#<=>(rhs) ⇒ Object
78 79 80 |
# File 'lib/tonal/log.rb', line 78 def <=>(rhs) rhs.kind_of?(self.class) ? logarithm <=> rhs.logarithm : logarithm <=> rhs end |
#inspect ⇒ String
Returns the string representation of Tonal::Log.
74 75 76 |
# File 'lib/tonal/log.rb', line 74 def inspect "#{logarithm}" end |
#step(modulo) ⇒ Tonal::Step
Returns the nearest step in the given modulo.
66 67 68 |
# File 'lib/tonal/log.rb', line 66 def step(modulo) Tonal::Step.new(modulo: modulo, log: self) end |