Class: Tonal::Hertz
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.a440 ⇒ Tonal::Hertz
440 Hz.
Instance Method Summary collapse
- #<=>(rhs) ⇒ Object
- #initialize(arg) ⇒ Tonal::Hertz constructor
-
#inspect ⇒ String
The string representation of Tonal::Hertz.
- #method_missing(op, *args, &blk) ⇒ Object
-
#to_f ⇒ Rational
Self as a float.
-
#to_r ⇒ Rational
Self as a rational.
Constructor Details
#initialize(arg) ⇒ Tonal::Hertz
11 12 13 14 |
# File 'lib/tonal/hertz.rb', line 11 def initialize(arg) raise ArgumentError, "Argument is not Numeric or Tonal::Hertz" unless arg.kind_of?(Numeric) || arg.kind_of?(self.class) @value = arg.kind_of?(self.class) ? arg.inspect : arg end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(op, *args, &blk) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/tonal/hertz.rb', line 53 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 result.kind_of?(Numeric) ? self.class.new(result) : result end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
4 5 6 |
# File 'lib/tonal/hertz.rb', line 4 def value @value end |
Class Method Details
.a440 ⇒ Tonal::Hertz
Returns 440 Hz.
20 21 22 |
# File 'lib/tonal/hertz.rb', line 20 def self.a440 self.new(440.0) end |
Instance Method Details
#<=>(rhs) ⇒ Object
49 50 51 |
# File 'lib/tonal/hertz.rb', line 49 def <=>(rhs) rhs.kind_of?(self.class) ? value <=> rhs.value : value <=> rhs end |
#inspect ⇒ String
Returns the string representation of Tonal::Hertz.
45 46 47 |
# File 'lib/tonal/hertz.rb', line 45 def inspect "#{value}" end |
#to_f ⇒ Rational
Returns self as a float.
37 38 39 |
# File 'lib/tonal/hertz.rb', line 37 def to_f value.to_f end |
#to_r ⇒ Rational
Returns self as a rational.
28 29 30 |
# File 'lib/tonal/hertz.rb', line 28 def to_r Rational(value) end |