Class: Alda::Chord
- Includes:
- EventList
- Defined in:
- lib/alda-rb/event.rb
Overview
A chord event. Includes Alda::EventList.
An Alda::EventContainer containing an Alda::Chord can be created using event list sugar. See Alda::EventList#method_missing.
Alda::Score.new do
p x{ c; e; g }.event.class # => Alda::Chord
end
The event contained by an Alda::EventContainer can become an Alda::Chord by using Alda::EventContainer#/.
Instance Attribute Summary
Attributes included from EventList
Attributes inherited from Event
Instance Method Summary collapse
-
#initialize(*events, &block) ⇒ Chord
constructor
:call-seq: new(*events, &block) -> Alda::Chord.
-
#to_alda_code ⇒ Object
:call-seq: to_alda_code() -> String.
Methods included from EventList
#==, #events_alda_codes, #has_variable?, #import, #method_missing, #on_contained, #to_a
Methods inherited from Event
#==, #detach_from_parent, #is_event_of?, #on_contained
Constructor Details
#initialize(*events, &block) ⇒ Chord
:call-seq:
new(*events, &block) -> Alda::Chord
There is an event list sugar invoking this method. See Alda::EventList#method_missing.
In most cases, events
should be empty. Note that events
cannot be specified using the sugar. block
is to be passed with the chord object as self
.
Alda::Score.new { piano_; x { c; -e; g } }.play
# (plays chord Cm)
762 763 764 765 766 |
# File 'lib/alda-rb/event.rb', line 762 def initialize *events, &block events.each { _1.parent = self if _1.is_a? Alda::Event } @events = events super &block end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Alda::EventList
Instance Method Details
#to_alda_code ⇒ Object
:call-seq:
to_alda_code() -> String
Overrides Alda::Event#to_alda_code.
Behaves differently for Alda 1 and Alda 2: because Alda 2 does not allow octave changes as part of a chord (something like a/>/c
, and we have to write a>/c
or a/>c
instead) (alda-lang/alda#383), the code generated by this method will omit the slash before an octave change.
Alda.generation = :v1
Alda::Score.new { a/o!/c; a/o5/c }.to_s # => "a/>/c a/o5/c"
Alda.generation = :v2
Alda::Score.new { a/o!/c; a/o5/c }.to_s # => "a>/c a o5/c"
784 785 786 787 788 789 790 791 792 793 794 795 796 |
# File 'lib/alda-rb/event.rb', line 784 def to_alda_code return events_alda_codes ?/ if Alda.v1? @events.each_with_index.with_object '' do |(event, i), result| if i == 0 # concat nothing elsif event.is_event_of? Alda::Octave result.concat ' ' unless event.num.empty? else result.concat '/' end result.concat event.to_alda_code end end |