Class: Stretto::MusicElements::Variable

Inherits:
MusicElement show all
Defined in:
lib/stretto/music_elements/variable.rb

Overview

Holds a value, identified by a name, that can be used in some elements in the pattern.

The syntax for setting a value is $name=vaue

For example, in a pattern

$SOME_VARIABLE=80 [SOME_VARIABLE]

the note will have a picth of 80, as defined by the variable SOME_VARIABLE

Value can be either a numeric value or another variable. Note that predefined variables are not constants; they can be overriden at any time, and the elements after the new definition will take its most recent value.

See Also:

Instance Attribute Summary collapse

Attributes inherited from MusicElement

#original_string, #pattern

Attributes included from Node

#next, #prev

Instance Method Summary collapse

Methods inherited from MusicElement

#build_music_string, #duration, #end_of_tie?, #start_of_tie?, #substitute_variables!, #to_s

Constructor Details

#initialize(string_or_options, pattern = nil) ⇒ Variable

Returns a new instance of Variable.



26
27
28
29
30
31
32
33
34
# File 'lib/stretto/music_elements/variable.rb', line 26

def initialize(string_or_options, pattern = nil)
  token = case string_or_options
    when String then Stretto::Parser.parse_variable!(string_or_options)
    else string_or_options
  end
  super(token[:text_value], pattern)
  @value = token[:value]
  @name  = token[:name]
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



24
25
26
# File 'lib/stretto/music_elements/variable.rb', line 24

def name
  @name
end

#valueObject (readonly)

Returns the value of attribute value.



24
25
26
# File 'lib/stretto/music_elements/variable.rb', line 24

def value
  @value
end

Instance Method Details

#to_fFloat?

Returns:

  • (Float, nil)


42
43
44
# File 'lib/stretto/music_elements/variable.rb', line 42

def to_f
  @value.to_f(@pattern)
end

#to_iInteger?

Returns:

  • (Integer, nil)


37
38
39
# File 'lib/stretto/music_elements/variable.rb', line 37

def to_i
  @value.to_i(@pattern)
end