Class: Oscillo::NumSignal

Inherits:
Signal
  • Object
show all
Defined in:
lib/oscillo/num_signal.rb

Overview

A signal that works on numeric values.

Instance Method Summary collapse

Methods inherited from Signal

#abort, #change, #dont_follow, #drop_repeats, #follow, #on_change, #sample_on, #source, #to_i, #to_s, #update, #value

Methods included from Enumerable

#all?, #any?, #collect, #count, #each, #find_all, #inject, #reject, #zip

Constructor Details

#initialize(*signals) {|*signals, self| ... } ⇒ NumSignal #initialize(start_value, *signals) {|*signals, self| ... } ⇒ NumSignal

Creates a new Signal with an optional initial value, signals to follow, and block. The block converts the values from any signals that this one follows to the value of this signal. If no block is given, the value will be an array of all the values of the signals that it follows (or if it is just following one signal, the value of that signal).

Defaults to 0 instead of nil.

Overloads:

  • #initialize(*signals) {|*signals, self| ... } ⇒ NumSignal

    Follows some number of other signals, changing when they change.

    Parameters:

    • signals (Signal)

      the signals to follow

    Yields:

    • (*signals, self)

      gives the new values of the signals to the block

  • #initialize(start_value, *signals) {|*signals, self| ... } ⇒ NumSignal

    Starts with an initial value and follows other signals when they change.

    Parameters:

    • start_value

      the initial value of this signal

    • signals (Signal)

      the signals to follow

    Yields:

    • (*signals, self)

      gives the new values of the signals to the block



6
7
8
# File 'lib/oscillo/num_signal.rb', line 6

def initialize(*, &block)
  @value = 0; super
end

Instance Method Details

#deltaSignal

The new signal gives the difference between the previous value and the current value of the signal.

Returns:



22
23
24
25
26
27
# File 'lib/oscillo/num_signal.rb', line 22

def delta
  prev = self.val
  self.class.new(self) do |v, s|
    v - prev.tap { prev = v }
  end
end

#div(signal) ⇒ Signal Also known as: /

The new signal is the division of the original signal and the given signal.

Parameters:

  • signal (Signal)

    the signal to divide

Returns:



71
72
73
# File 'lib/oscillo/num_signal.rb', line 71

def div(signal)
  self.class.new(self, signal) { |a, b| a / b }
end

#minus(signal) ⇒ Signal Also known as: -

The new signal is the difference of the original signal and the given signal.

Parameters:

  • signal (Signal)

    the signal to subtract

Returns:



51
52
53
# File 'lib/oscillo/num_signal.rb', line 51

def minus(signal)
  self.class.new(self, signal) { |a, b| a - b }
end

#mod(signal) ⇒ Signal Also known as: %

The new signal is the modulus of the original signal and the given signal.

Parameters:

  • signal (Signal)

    the signal to mod

Returns:



81
82
83
# File 'lib/oscillo/num_signal.rb', line 81

def mod(signal)
  self.class.new(self, signal) { |a, b| a % b }
end

#plus(signal) ⇒ Signal Also known as: +

The new signal is the sum of the original signal and the given signal.

Parameters:

  • signal (Signal)

    the signal to sum

Returns:



41
42
43
# File 'lib/oscillo/num_signal.rb', line 41

def plus(signal)
  self.class.new(self, signal) { |a, b| a + b}
end

#pow(signal) ⇒ Signal Also known as: **

The new signal is the original signal raised to the power of the given signal.

Parameters:

  • signal (Signal)

    the signal to raise to the power of

Returns:



91
92
93
# File 'lib/oscillo/num_signal.rb', line 91

def pow(signal)
  self.class.new(self, signal) { |a, b| a ** b }
end

#productSignal

The new signal gives the running product of all values of the original signal.

Returns:



33
34
35
# File 'lib/oscillo/num_signal.rb', line 33

def product
  self.inject(:*)
end

#sumSignal

The new signal gives the running total of all values of the original signal.

Returns:



14
15
16
# File 'lib/oscillo/num_signal.rb', line 14

def sum
  self.inject(:+)
end

#times(signal) ⇒ Signal Also known as: *

The new signal is the product of the original signal and the given signal.

Parameters:

  • signal (Signal)

    the signal to multiply

Returns:



61
62
63
# File 'lib/oscillo/num_signal.rb', line 61

def times(signal)
  self.class.new(self, signal) { |a, b| a * b }
end