Here’s the latest version of my Ruboto presentation… An earlier version of these slides was used by Charlie at JRubyConf 2010, and I updated them for my talk at Philly Emerging Tech.
I had a spectacular time live-coding on a visualizer for the third Desert Bloom PHX party tonight. I learned a few lessons while doing it, though, which I thought I’d share while they’re still fresh in my mind. I’m hoping these will help anyone who needs to supply visuals for parties.
If you’re randomly generating settings, some of them are gonna look like crap. Code up a way to quickly serialize presets to disk that you can switch between when you have an audience. (You probably don’t need a file save dialog, just a save button. Think “that’s pretty, save that” and “load up the next preset, whatever it is”.)
Turn key settings of your visualizer into a Web service or other network service. I made the onscreen text and “next preset” functions (among others) accessible over the network, then used a script on my smartphone as a remote control, leaving me free to roam the room. This also enables something as simple as control from a second terminal window on the same machine.
class <<f
attr_accessor :text, :text_separator, :max_font_size, :min_font_size, :reset_odds, :shape_type, :max_shape_count, :listen_url
attr_accessor :max_size
end
In honor of Why The Lucky Stiff’s contributions to the fun side of the Ruby community, whyday.org includes a challenge to “see how far you can push some weird corner of Ruby”. I can think of few corners of Ruby that are weirder (or more fun) than DRb.
Distributed RuBy (DRb) is, in my opinion, the most underrated portion of the Ruby standard library. It lets you take a Ruby class and network-enable it with almost no additional code (and without modifying the original class). Back in 2006 when I was considering whether to learn Ruby or not, I took one look at DRb and realized that a language that made such things possible was probably a language worth knowing.
I made this screencast to show off how powerful DRb is, and how easy it is to get started. We create a simple drawing canvas in Tk, then use DRb to network-enable it and draw to it from a 4-line client. We finish with a Ruby client that runs on Android via the Ruboto environment. And along the way, we cover a little basic security to help keep you safe (this is a network app, after all).
Here’s the complete code for the server:
require 'tk'
require 'drb'
$SAFE = 1
canvas = TkCanvas.new(:width => 800, :height => 600)
canvas.pack
class RemoteCanvas
def initialize(canvas)
@canvas = canvas
end
def circle(x, y)
TkcOval.new(@canvas, x, y, x + 40, y + 40)
end
end
DRb.start_service("druby://192.168.0.100:9000", RemoteCanvas.new(canvas))
canvas.mainloop
And here’s the complete touchscreen client for Ruboto:
require "ruboto.rb"
confirm_ruboto_version(4, false)
java_import "org.ruboto.embedded.RubotoView"
require 'drb'
DRb.start_service
$activity.start_ruboto_activity "$druby" do
setup_content do
@service = DRbObject.new(nil, "druby://192.168.0.100:9000")
RubotoView.new($druby)
end
handle_touch_event do |event|
@service.circle(event.get_x, event.get_y)
end
end
Enjoy, and of course feel free to post questions and comments below. And if you’re looking for your own way to celebrate WhyDay, why not pick your favorite library and network-enable it?
Ruboto 0.3 added TouchListener to its event handlers. Here’s my experiment with adding multitouch input to the Ruby On Acid demo…
When it starts up (or the factory resets), all attributes are controlled by an ExampleFactory, meaning they’ll randomly be assigned to LoopFactories, SineFactories, RandomWalkFactories, etc. Touch the screen, though, and control of the drawing x/y position will be given to the touchscreen. Touch in a second place, and control of random attributes will go to your second finger: r/g/b channel, opacity, width, or height.
After dissecting the demos that come with Ruboto 0.2 and figuring out how to upload a library and runner script to an Android emulator (hint: “$ adb push . /sdcard/jruby/subdirname”), it only took an hour or so to create a working Ruby On Acid demo. And what worked on the emulator immediately worked when uploaded to the phone.
I'm a software developer whose personal motto is to be vicariously lazy: I believe I can make everyone's lives easier through programming. I'm a Ruby and Java developer and have presented at conferences including RubyConf, mountain.rb, and Philly Emerging Tech. I currently maintain several open-source Ruby libraries, which you can find on my GitHub page. You can find me on Twitter as @jaymcgavren.