Networked Drawing Canvas in DRb
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).
http://www.youtube.com/watch?v=Ca6CBm4bSU8
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
Here’s our (tiny) sample client:
require 'drb'
DRb.start_service
canvas = DRbObject.new(nil, "druby://192.168.0.100:9000")
canvas.circle(300, 400)
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?
Read more...Making You Happy
“Do Ophelia!”
“Please do Tigerlily!”
…came the encore requests from rapt audience members at the Natalie Merchant concert. And with the kind but commanding tone of someone who knows how to work a crowd, Natalie responded:
“To those of you asking for particular songs, don’t worry, we have something planned. But that makes us feel bad, because we might not know how to do the songs you ask for.” [I assume she meant the ensemble of 8-odd strings, woodwinds, etc. onstage, because I doubt she herself had forgotten the lyrics to “Ophelia”.]
“But this is our promise to you - we’ll make you happy.”
I think that’s a good model for every request, whether you’re asking someone to paint you a painting, code you an application, or sing you a song: Tell them the bare minimum to make you happy. Let them figure out the details, in a way that utilizes their strengths (which they know better than you). Instead of getting a clumsy interpretation of what you were imagining, you may find you’ve commissioned a work of art.
Read more...Ruboto On Acid - Multitouch
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.
Read more...Screencasting checklist...
I’ve recorded one too many screencasts in an unreadably small font. I thought I’d made a pre-recording checklist, but I seem to have lost it. So here we go again, and this time I’ll share it with the Web at large:
Ensure iShowU is on "Rails Demo" preset: Record microphone, system audio off Normal frame rate: 5 Compression: Apple Animation Capture Size: 800 x 600 Show capture guide whether recording or not Follow mouse cursor: false Edit recording area so it's in upper left, touching underside of system menu Set iShowU keyboard shortcuts: Start capture: Cmd-Opt-Ctrl-3 Pause capture: Cmd-Opt-Ctrl-4 Size windows to fit within capture area: Web browser Shell(80 columns x 30 rows) Editor Increase font size to 20-point or larger: Web browser ("Zoom In" x 2) Shell Editor Plain background as desktop Close all possible appsRead more...
Pat Maddox - I Love Ruby
From Pat’s MountainWest RubyConf talk. I’m giving a presentation on Rails at work (a JEE shop), and I figured this would help convince my audience. But it also says what many in the Ruby community already feel, so I figured I’d share it.
Like the original, this is released under the Creative Commons Attribution-ShareAlike license.
Read more...