Created
October 30, 2018 23:48
-
-
Save hopbit/e5c582e3659a5d9f1cb0d13230883784 to your computer and use it in GitHub Desktop.
Passing lambda as a constructor parameter in Sonic Pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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