Live Coding

Martin Prout edited this page Jun 2, 2018 · 28 revisions

Automatically Reload your Sketch with k9 watch

k9 --watch will keep an eye on the source file of your sketch. Whenever you save a change to it, it'll reload your running code, so you can try out your ideas quickly. k9 --watch is the best way to quickly prototype a sketch. To guard against running watch mode in a top level directory, we limit the number of files to watch to 20. To change the number you want to watch, edit ~/.jruby_art/config.yml to include MAX_WATCH: 100 say.

TIP

If you use constants in your sketch, use ||= to define them, and avoid warnings of already defined constants.

Impress your friends with live coding using k9 --live

JRubyArt is a playground for live coding. Sketches can be loaded into an interactive code session (pry) using k9 --live. Once your sketch is running, the full powers of Ruby meta-programming are there for you to use. Methods and classes can be redefined on the fly, arguments passed, values changed and all that.

>> k9 --live contributed/jwishy.rb
[1] pry(#<Sketch>)> def bluish
[1] pry(#<Sketch>)*   sin(y_wiggle)
[1] pry(#<Sketch>)* end  
=> :bluish
[2] pry(#<Sketch>)> @back_color = 0.5, 0.5, 0.5
=> [0.5, 0.5, 0.5, 0.5]

This opens up the Wishy Worm example in a window, and redefines the bluish method to return a function of the y_wiggle. The blue begins to pulse. We then set background color by changing the back_color variable, you also need to move alpha slider to 0.03 or so to get the following look to your sketch:-

jwishy pic

To view a bunch of methods available ls in the pry console, pry aficionados will know more.. (and that there are currently limitations of using pry with jruby). To use the pry edit mode create a class wrapped sketch. See below for usage uses your default editor (vim or emacs are probably the best, preference can be set in .pryrc see pry documentation) where the editor opens with the sketch draw method. Sketch updates on save followed by closing editor unless you have set set up a server see below for emacs, but I'm pretty sure this can be also done with vim (with or without tmux).

[1] pry(#<Fred>)> edit -p Fred#draw

Live Coding Using Emacs Client and Pry

First you should set the editor to 'emacsclient' as suggested on pry wiki. NB this is different from the customised emacs setup.

Pry.config.editor = "emacsclient"

Now fire up emacs and set it to client mode

M-x server-start

Then start the live coding on your sketch (I find a class wrapped sketch works best)

k9 --live Fred.rb # in a console

Once the pry session starts up in the console you can then edit the draw method say:-

edit -p Fred#draw

When you have completed editing

C-x #

and save buffer, the sketch refreshes with your changes, and you are returned to pry console (and you can repeat at your will, with your changes saved in the temporary file) see also a customised emacs setup.

Live Coding Using Vim and Pry

When editing a sketch in vim use the following command to enter live mode (but it may not be good idea)

:!k9 --live %

This opens up the pry shell as with emacs in pry shell enter following pry shell

edit -p MySketch#draw

this opens up a new vim on the draw method, on :wq exit of vim the sketch is saved to temp and reloads. Possibly because you started off in vim, you can return and re-edit the draw method, which retains your last changes...

see video here

Thanks Carlos Rocha