Leap + Ruby

I really wanted to use the Leap SDK in Ruby after I recently got my Developer Kit.

I tried writing an FFI lib (to support all Ruby versions) but noticed it might not be sufficient.

Then I tried writing a C-extension, but realized I don’t have enough time for that right now.

Finally I tried writing a wrapper with JRuby instead (the Leap SDK includes Java libs) and it was so simple.

Pretty excited to do more with this library. Many thanks to Leap and JRuby!

https://github.com/tiegz/LeapRuby

I recently reprogrammed the MaKeyMaKey to control Roy the Robot’s hand, which just meant turning 6 of the input pins on MaKeyMaKey into 6 output pins, and using the Arduino Servo library to control them (my fork of the firmware is here).

I also heard about a MaKeyMaKey contest on UncommonGoods & put together this video to demo the hand.

Unfortunately, the servos weren’t strong enough to play the piano :(

Hidden Secrets of the Rails Dev: Gotchas while Migrating from 1.8 to 1.9

There are quite a few good articles on the web about changes in Ruby 1.9. 

Here are just a few changes that tripped me up a bit while making the upgrade in a Rails app:

  • String#each has been removed: use String#each_line instead
  • String#to_a was removed: use .lines.to_a instead
  • "Some string" + Exception.new("hey that's bad") throws a TypeError now: force the exception using to_s
  • [1,2,3].to_s now returns "[1,2,3]" instead of "123": ie, now it’s inspect instead of join
  • incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string): specify utf8 encoding with switch: /blah/ -> /blah/u, or Regexp.new("blah", Regexp::FIXEDENCODING). Read more about 1.9 and encoding at Shades of Gray 
  • 1.9.2+ doesn’t include “.” in the path ($:) anymore: use the new require_relative, or if you need to just add it explicitly: $: << "." if RUBY_VERSION =~ /1\.9/
  • 1.9 is less forgiving for spaces before args now: instead of getting a warning for the space in link_to ("home", "/"), 1.9 will now throw an error
  • 1.9 changed Method introspection: instead of method(:to_s).__file__ and method(:to_s).__line__, use method(:to_s).source_location instead
  • 1.9.3 adds new YAML parser: basically this breaks any serialized rows you might have saved in 1.9 (with Syck) with Rails’ serialization attributes. Solution here.
  • 1.9 changes Date#parse format from “MM/DD/YYY” to “DD/MM/YYYY”: this cascades to Rails’ DateTime#parse

Also, try this String monkey patch if you’re transitioning between 1.8 and 1.9. Call this whenever you need to ensure a string is utf8:

Dreams of ARel: Visualize your ActiveRecord queries

I’m currently trying to track down an ActiveRecord 3.2.* bug related to generating a count query, and thought it might be useful see a visualization of the ARel object that this query generates.

It turns out this is pretty easy!

ARel already implements a method called “to_dot” that converts your ARel into DOT, which is readable by Graphviz.

Building an API: Rails 3 vs Grape

I’ve been pretty impressed with the Grape Rack library for building an API, but I was curious how it stacks up against Rails 3, now that Rails has been Rack-ified and has steadily become more hospitable to APIs.

Using an existing Rails app with a Grape API mounted as middleware, I ran a benchmark against a vanilla API with this environment:

  • Grape 0.1.5
  • Rails 3.0.10
  • Passenger 3.0.5
  • Ruby Enterprise Edition (ruby 1.8.7), patchlevel 334
  • Macbook Pro, 2.4 GHz / 8GB, OS X 10.6.8

And it looks like Grape wins in this particular environment:

  • Grape averages 47.21 req/sec
  • Rails 3 averages 31.41  req/sec

Grape vs Rails 3 API performance

Here’s how I implemented both scenarios:

Hidden Secrets of the Rails Dev: Gotchas While Migrating from 2 to 3

Upon migrating from Rails 2 to 3, one might happen across some “gotchas”. I present a few such that you may heed caution.

ActiveSupport::Callbacks#run_callbacks

This method takes a &block parameter. In Rails 2, &block will run after each callback, and halt the chain if it returns false. In Rails 3, &block will run after the :before/:around callbacks and before the :after callbacks. More has changed about callbacks, so please read the docs.

Mail#header

ActionMailer in Rails 3 uses a different underlying library than 2: mail instead of tmail. Setting headers in the new library will accumulate headers rather than replacing them:

m['X-Something'] = 1
m['X-Something'] = 2
m.header['X-Something'] = 3

Now m has 3 values for the X-Something header: 1, 2 and 3.

ActiveRecord::Base#touch

In Rails 2, touch() ultimately calls save!() and runs through all the validation and callbacks. In Rails 3, touch() saves the record using update_all(), so no validations are performed and only the new after_touch() callback is run.

ActiveRecord#order

In Rails 2, queries are ordered like this:

User.first(:order => "name")

In Rails 3, AREL gives us a new method for queries called order(), but remember that it will accumulate order values instead of replacing them:

User.order("name").order("id").first
# ... ORDER BY name, id .... #

Remember to use reorder() to reset the value:

User.order("name").reorder("id")
# ... ORDER BY id ... #

Reorder your Authlogic persistence callbacks

One thing lacking from ActiveSupport::Callbacks, imo, is a way to reorder callbacks.

For instance, it would be nice if you could define the order in which Authlogic (which uses ActiveSupport::Callbacks) tries to persist a session (ie ‘remember’ cookie, then ‘session’ cookie, then ‘http_auth’, and so on).

Here’s some code to put at the end of your session models to prioritize their callback chains…

For Rails 2:

For Rails 3:

(link)

“SayIt ☎”, a New Pronunciation Tool!

Have you ever argued with someone over the pronunciation of a word? Let “the cloud” resolve your issue!

“SayIt ☎” is my new Kynetx/Twilio/Forvo mashup that does 1 simple thing: pronounce words

Try it… send “plasmometric” to 917.740.1911 … 

Import your data from RunnersWorld to RunKeeper!

If you need to move your information from Runner’s World into RunKeeper, but like me have run into the problem that RunKeeper doesn’t have an api (yet?) and doesn’t let you import data, then fear not!

If you like living “on the edge”, try running my script by following these steps:

  • Download the Ruby script here to any directory
  • You’ll have to run a command to get the necessary Ruby libraries: sudo gem install active_support hpricot json
  • Login to your Training Log at Runner’s World
  • Select ‘Tools’
  • Select ‘Export’ (in XML format), and click ‘Download’
  • Copy the downloaded file into the directory where you saved the Ruby script
  • Run this: ruby run_hacker.rb rwFile.xml (where ‘rwFile.xml’ is the file you downloaded from Runner’s World)
  • Finally, you’ll have to enter your RunKeeper email, RunKeeper password, and confirm every event that you would like to import.

CATCHES: 

  • For some reason the total distance in RunKeeper is recorded as “0.0” for imported Mapped runs (aka GPS runs). You can fix this by visiting RunKeeper, clicking “Edit Activity” for each Mapped run, move one of the points *very slightly* and then click “Save”. That should reset the distance correctly.
  • This should work for RunningAHEAD too, but I haven’t yet figured out how to get the maps from a RunningAHEAD account w/out logging in first.

This script has worked for me so far; let me know if you try it and encounter any problems! I’d love to abstract this into a real api wrapper, but I am on the verge of wasting too much time.

Enjoy.

(comments or bugs?)

New Name/Site for Kynetx App!!

I found some tonight to return to Similar Stories Different Take and make a few changes:

  1. renamed to Different Take (thx Cody)
  2. added support for Bloomberg News!!! 
  3. more polishing of the news-search algorithm
  4. website is now at http://different-take.com
  5. added browser extensions (courtesy of Kynetx)… the options for getting your balance of news are:
  • Bookmarklet
  • Azigo card
  • Firefox extension
  • Chrome extension
  • Internet Explorer extension

(visit the site to install)

And if you happen to be in Utah tomorrow (Apr. 27), check out the Kynetx Impact Conference!

Different Take

Accent theme by Handsome Code


view archive



Ask me anything