Class: Tonal::Log

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable
Defined in:
lib/tonal/log.rb

Direct Known Subclasses

Log2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logarithmand: nil, logarithm: nil, base: nil) ⇒ Tonal::Log

Examples:

Tonal::Log.new(logarithmand: 3/2r, base: 2) => 0.5849625007211562

Parameters:

  • logarithmand (defaults to: nil)
  • logarithm (defaults to: nil)
  • base (defaults to: nil)

Raises:

  • (ArgumentError)


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

#baseObject (readonly)

Returns the value of attribute base.



7
8
9
# File 'lib/tonal/log.rb', line 7

def base
  @base
end

#logarithmObject (readonly)

Returns the value of attribute logarithm.



7
8
9
# File 'lib/tonal/log.rb', line 7

def logarithm
  @logarithm
end

#logarithmandObject (readonly)

Returns the value of attribute logarithmand.



7
8
9
# File 'lib/tonal/log.rb', line 7

def logarithmand
  @logarithmand
end

Class Method Details

.baseObject



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

#inspectString

Returns the string representation of Tonal::Log.

Examples:

Tonal::Log.new(3/2r, base: 2).inspect => "0.5849625007211562"

Returns:

  • (String)

    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.

Examples:

Tonal::Log.new(3/2r, base: 2).step(12) => 7\12

Returns:

  • (Tonal::Step)

    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

#to_cents(precision: Tonal::Cents::PRECISION) ⇒ Tonal::Cents

Returns the cents scale logarithm.

Examples:

Tonal::Log.new(logarithmand: 3/2r, base: 2).to_cents => 701.9550008653874

Returns:

See Also:



58
59
60
# File 'lib/tonal/log.rb', line 58

def to_cents(precision: Tonal::Cents::PRECISION)
  Tonal::Cents.new(log: self, precision: precision)
end