簡體   English   中英

在Ruby中'<<'運算符有什么用

[英]What are the uses of the '<<' operator in ruby

我有這個示例代碼,從本質上來說,這只是用於mdii文件的一些基本類。

class Array
    def to_midi(file, note_length='eighth')

        midi_max = 108.0
        midi_min = 21.0

        low, high = min, max
        song = MIDI::Sequence.new

        # Create a new track to hold the melody, running at 120 beats per minute.
        song.tracks << (melody = MIDI::Track.new(song))
        melody.events << MIDI::Tempo.new(MIDI::Tempo.bpm_to_mpq(120))

        # Tell channel zero to use the "piano" sound.
        melody.events << MIDI::ProgramChange.new(0, 0)

        # Create a series of note events that play on channel zero.
        each do |number|
            midi_note = (midi_min + ((number-midi_min) * (midi_max-low)/high)).to_i
            melody.events << MIDI::NoteOnEvent.new(0, midi_note, 127, 0)
            melody.events << MIDI::NoteOffEvent.new(0, midi_note, 127,
            song.note_to_delta(note_length))
        end

        open(file, 'w') { |f| song.write(f) }
    end
end
class TimedTrack < MIDI::Track
    MIDDLE_C = 60
    @@channel_counter=0

    def initialize(number, song)
        super(number)
        @sequence = song
        @time = 0
        @channel = @@channel_counter
        @@channel_counter += 1
    end

    # Tell this track's channel to use the given instrument, and
    # also set the track's instrument display name.
    def instrument=(instrument)
        @events << MIDI::ProgramChange.new(@channel, instrument)
        super(MIDI::GM_PATCH_NAMES[instrument])
    end

      # Add one or more notes to sound simultaneously. Increments the per-track
      # timer so that subsequent notes will sound after this one finishes.
    def add_notes(offsets, velocity=127, duration='quarter')
        offsets = [offsets] unless offsets.respond_to? :each
        offsets.each do |offset|
            event(MIDI::NoteOnEvent.new(@channel, MIDDLE_C + offset, velocity))
        end
        @time += @sequence.note_to_delta(duration)
        offsets.each do |offset|
            event(MIDI::NoteOffEvent.new(@channel, MIDDLE_C + offset, velocity))
        end
        recalc_delta_from_times
    end

      # Uses add_notes to sound a chord (a major triad in root position), using the
      # given note as the low note. Like add_notes, increments the per-track timer.
    def add_major_triad(low_note, velocity=127, duration='quarter')
        add_notes([0, 4, 7].collect { |x| x + low_note }, velocity, duration)
    end

    private

        def event(event)
            @events << event
            event.time_from_start = @time
        end
end

除了使用<<運算符的行外,大多數代碼對我來說都是完全有意義的,根據我的所有研究,使用<<的唯一原因是在您定義一個單例類時。 那么在此代碼中使用<<具體方式是什么?

https://github.com/jimm/midilib
MIDI::Track是包含事件數組的軌道。

因此,通過<<將事件添加到軌道中。 它與melody.events.push(MIDI::NoteOnEvent.new(0, midi_note, 127, 0))

<<也可用於移位操作

http://calleerlandsson.com/2014/02/06/rubys-bitwise-operators/

<<運算符既可以用於按位運算(在這里不太可能),也可以在類內部重載以匹配某些行為。 在這種情況下,(可能)有一個Array對象,因此該event通過此運算符推入@events數組。 有關此用例的更多信息,請參見此處

請注意,將來您可能會遇到使用該運算符的其他情況,而不是每次都意味着相同的情況-它取決於庫創建者。 現在想到的另一個用例可以是ActiveRecord Relationships,例如has_many ,您還可以使用此運算符將對象立即推入關系並保存。 可以在api文檔中找到有關此內容的更多信息。 快速樣本:

class User < ActiveRecord::Base
  has_many :posts
end

class Post < ActiveRecord::Base
  belongs_to :user
end

然后可以在代碼中的某處使用:

@post = Post.find(10)
@user = User.find(1)
@user.posts << @post
# automatically save the @post object with updated foreign key

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM