class Alda::EventContainer

The class for objects containing an event.

Alda::EventContainer objects are literally everywhere if you are a heavy user of event list sugars. See Alda::EventList#method_missing.

Attributes

count[RW]

The repetition counts. nil if none.

Alda::Score.new do
  p((c*2).count)   # => 2
  p((d*3*5).count) # => 15
end
event[RW]

The contained Alda::Event object.

 Alda::Score.new do
   p c.event.class      # => Alda::Note
   p((e/g).event.class) # => Alda::Chord
   p((a b).event.class) # => Alda::Sequence
end
labels[RW]

The repetition labels. Empty if none.

Alda::Score.new do
  p((c%2).labels)        # => [2]
  p((c%[2,4..6]).labels) # => [2, 4..6]
end

Public Class Methods

new(event, parent) → Alda::EventContainer click to toggle source

Creates a new Alda::EventContainer. Invokes on_containing.

event is the Alda::Event object to be contained.

parent is the Alda::EventList object containing the event.

# File lib/alda-rb/event.rb, line 116
def initialize event, parent
  @event = event
  @parent = parent
  @labels = []
  on_containing
end

Public Instance Methods

container % labels → container click to toggle source

Marks alternative endings.

Alda::Score.new { (b a%1)*2 }.to_s
# => "[b a'1]*2"
# File lib/alda-rb/event.rb, line 177
def % labels
  labels = [labels] unless labels.is_a? Array
  @labels.replace labels.to_a
  self
end
container * num → container click to toggle source

Marks repetition.

For examples, see %.

# File lib/alda-rb/event.rb, line 164
def * num
  @count = (@count || 1) * num
  self
end
container / other → container click to toggle source

Makes event an Alda::Chord object.

Alda::Score.new { piano_; c/-e/g }.play
# (plays the chord Cm)

If the contained event is an Alda::Part object, makes event a new Alda::Part object.

Alda::Score.new { violin_/viola_/cello_; e; f; g}.play
# (plays notes E, F, G with three instruments simultaneously)
# File lib/alda-rb/event.rb, line 137
def / other
  other.detach_from_parent
  @event =
      if @event.is_a? Alda::Part
        Alda::Part.new @event.names + other.event.names, other.event.arg
      else
        Alda::Chord.new @event, other.event
      end
  self
end
event=(event) → event click to toggle source

Sets event and invokes on_containing.

# File lib/alda-rb/event.rb, line 188
def event= event
  @event = event
  on_containing
  @event
end
(missing method) → obj click to toggle source

Calls method on event.

Note that if the method of event returns event itself, the method here returns the container itself.

Alda::Score.new do
  container = c
  p container.class              # => Alda::EventContainer
  p container.respond_to? :pitch # => false
  p container.pitch              # => "c"
  p container.respond_to? :+@    # => false
  p((+container).class)          # => Alda::EventContainer
  p to_s                         # => "c+"
end
# File lib/alda-rb/event.rb, line 222
def method_missing(...)
  result = @event.__send__(...)
  result = self if result == @event
  result
end
on_containing() click to toggle source

A callback invoked in event= and ::new.

# File lib/alda-rb/event.rb, line 196
def on_containing
  if @event
    @event.container = self
    @event.parent = @parent
    @event.on_contained
  end
end
to_alda_code() click to toggle source
# File lib/alda-rb/event.rb, line 148
def to_alda_code
  result = @event.to_alda_code
  unless @labels.empty?
    result.concat ?', @labels.map(&:to_alda_code).join(?,)
  end
  result.concat ?*, @count.to_alda_code if @count
  result
end