Skip to content

Instantly share code, notes, and snippets.

@hopbit
Created October 30, 2018 23:48
Show Gist options
  • Save hopbit/e5c582e3659a5d9f1cb0d13230883784 to your computer and use it in GitHub Desktop.
Save hopbit/e5c582e3659a5d9f1cb0d13230883784 to your computer and use it in GitHub Desktop.
Passing lambda as a constructor parameter in Sonic Pi
test = lambda { |ctx|
puts "#{ctx.class}"
ctx.notes.each do |note|
play note, amp: 0.5
sleep 0.25
end
}
class TestMelody
attr_reader :notes
def initialize(block) # accepts a code block
@notes = [:C5,:D5,:E5,:F5,:G5,:A5,:B5,:C6]
@block = block
end
def play
@block.call(self) # calls code block with reference to itself
end
end
live_loop :testing do
mel = TestMelody.new(test)
mel.play
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment