Class: ShlispTools::Ratio

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/shlisp_tools/ratio.rb

Overview

A Shlisp-oriented representation of a pitch in just/rational intonation. Numerators are “numes” and denoninators are “denos” as is customary for Shlisp. All terms 0 < n < 256 (“arab” mode in Shlisp).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nume, deno, _mul = nil) ⇒ Ratio

Returns a new instance of Ratio.



16
17
18
19
20
21
# File 'lib/shlisp_tools/ratio.rb', line 16

def initialize(nume, deno, _mul=nil)
  @nume = nume
  @deno = deno
  _set_rat
  mul(_mul)
end

Instance Attribute Details

#denoObject (readonly)

denominator



12
13
14
# File 'lib/shlisp_tools/ratio.rb', line 12

def deno
  @deno
end

#numeObject (readonly)

numerator



10
11
12
# File 'lib/shlisp_tools/ratio.rb', line 10

def nume
  @nume
end

#ratObject (readonly)

rational form



14
15
16
# File 'lib/shlisp_tools/ratio.rb', line 14

def rat
  @rat
end

Instance Method Details

#<=>(other) ⇒ Object

Comparator, for sorting



83
84
85
# File 'lib/shlisp_tools/ratio.rb', line 83

def <=>(other) #:nodoc:
  @rat <=> other.rat
end

#dObject

Get the deno(minator).



36
37
38
# File 'lib/shlisp_tools/ratio.rb', line 36

def d
  @deno
end

#d=(deno) ⇒ Object

Set the deno(minator).



41
42
43
44
45
# File 'lib/shlisp_tools/ratio.rb', line 41

def d=(deno)
  @deno = deno
  _set_rat
  d
end

#each {|@rat| ... } ⇒ Object

Iterator

Yields:



78
79
80
# File 'lib/shlisp_tools/ratio.rb', line 78

def each #:nodoc:
  yield @rat
end

#mul(factor) ⇒ Object

Multiply both nume and deno by same factor for Shnth amplitude scaling.



48
49
50
51
52
53
54
55
# File 'lib/shlisp_tools/ratio.rb', line 48

def mul(factor)
  if factor
    factor = factor.abs
    @nume = (@nume * factor) % LIMIT
    @deno = (@deno * factor) % LIMIT
  end
  self
end

#nObject

Get the nume(rator).



24
25
26
# File 'lib/shlisp_tools/ratio.rb', line 24

def n
  @nume
end

#n=(nume) ⇒ Object

Set the nume(rator).



29
30
31
32
33
# File 'lib/shlisp_tools/ratio.rb', line 29

def n=(nume)
  @nume = nume
  _set_rat
  n
end

#reset!Object

Return to the canonical (reduced) value.



62
63
64
65
# File 'lib/shlisp_tools/ratio.rb', line 62

def reset!
  @nume = @rat.numerator
  @deno = @rat.denominator
end

#scale(factor) ⇒ Object

:nodoc:



57
58
59
# File 'lib/shlisp_tools/ratio.rb', line 57

def scale(factor) #:nodoc:
  mul(factor)
end

#to_rObject

Rational representation



73
74
75
# File 'lib/shlisp_tools/ratio.rb', line 73

def to_r
  @rat
end

#to_sObject

String representation



68
69
70
# File 'lib/shlisp_tools/ratio.rb', line 68

def to_s
  "#{@nume}/#{@deno}"
end