ruby

Code Koans Pecha Kucha

The Geek ‘n’ Eat group at work had a Pecha Kucha day, and I prepped a quick presentation on learning Ruby/Clojure/Scala/Javascript through koans. (It’s also secretly about flow and how to achieve it in your coding.) I had recently learned how to record audio and export video from Keynote, and applied my newfound knowledge to do a Youtube-ready version:

development
ruby
testing

Permalink

mountain.rb wrapup book…

The mountain.rb (henceforth to be known as Rocky Mountain Ruby) Chronicles are finally out. Pretty darn honored to occupy a page opposite Jim Weirich. (Many other illustrious names in there too.) Great wrapup of a great event.

ruby

Permalink

CC-licensed Ruboto presentation…

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.

Pre-rendered HTML

Source (for Showoff presentation software)

These are Creative Commons-licensed, so you’re encouraged to steal these slides for use at your favorite technology user groups!

android
development
ruby

Permalink

RubyOnAcid: More fun with enhanced CombinationFactory…

This time, hooked up to Ruby-Processing. I should have logged the factories assigned to each attribute, but didn’t, so the titles are my best guesses as to how they were generated.

color channel on LoopFactorySineFactory plus… what?the random stache

Lissajous plus RoundingSine x, Sine2 y, Loop redLissajousFactory

Factories chosen are still completely random, so I’m throwing away ten times as many images as I’m keeping. I bet the really mind-blowing stuff will happen when I add a genetic algorithm.

development
ruby

Permalink

RubyOnAcid: CombinationFactory::REBOUND…

Added a simple constraint type to CombinationFactory that causes values to “bounce” off of the upper and lower bounds:

-0.2 -> 0.2
-0.1 -> 0.1
0.0 -> 0.0
0.1 -> 0.1
0.2 -> 0.2
...
0.9 -> 0.9
1.0 -> 1.0
1.1 -> 0.9
1.2 -> 0.8
...
1.9 -> 0.1
2.0 -> 0.0
2.1 -> 0.1

So when scaled to screen dimensions, for example, x and y coordinates can seem to “rebound” off the edges of the screen, or an object can “bounce” back and forth between two colors, or minimum and maximum sizes. The SineFactory did some of this before, but this gives an abrupt reversal of direction instead of a smooth curve (and sometimes the former looks cooler).

Also, I had the ExampleFactory randomly choose an operation and a constraint mode for its component CombinationFactory. Since it only used the defaults before, and since it seems I never use any functionality that isn’t wrapped up in the ExampleFactory, these rather cool features were woefully under-utilized. This should fix that.

Here’s a few sample SVG files, generated with the updated ExampleFactory:

2010-11-09_232353.svg
2010-11-09_232419.svg
2010-11-09_232816.svg
2010-11-10_011839.svg

development
ruby

Permalink

This is cheating at live-coding - I just replayed my re-do buffer in TextMate. I figured the result would be interesting if I ever want to go back and analyze my technique, though.

train_of_thought.mov

development
ruby

Permalink

Baddest. Conference. Badges. Evar.

Baddest. Conference. Badges. Evar.

development
ruby

Permalink

Live-Coding Lessons Learned

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”.)

def save_preset
    t = Time.new
    file_name = sprintf("%04d-%02d-%02d_%02d%02d%02d", t.year, t.month, t.day, t.hour || 0, t.min || 0, t.sec || 0)
    File.open(File.join(preset_directory, file_name), "w") do |file|
        file.print YAML.dump [source_factories, @assigned_factories]
    end
    file_name
end
def next_preset
    directory = Dir.open(preset_directory)
    files = directory.entries[2..-1]
    @preset_index = @preset_index ? @preset_index + 1 : 0
    @preset_index = 0 if @preset_index > files.length - 1
    path = File.join(preset_directory, files[@preset_index])
    source_factories, @assigned_factories = YAML.load_file(path)
    path
end

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.

screen-2010-09-18-at-12412-am.png

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

Continue Reading »

android
development
ruby

Permalink

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).

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?

android
development
ruby

Permalink

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.

Continue Reading »

android
development
ruby

Permalink