Friday, August 31, 2007

Ruby Disk Based Hash - DBM

I've lost this on google for a year and now I found it! it took 30min of googleing (yes I suck at searching)

require 'dbm'

db ='data/test')
db['hello'] = "Justin is really happy he found this"
db['super'] = ":-)"

** data saved to disk directly

db ='data/test')
=> "Justin is really happy he found this"

Its pretty fast, at least I'm happy.

Sunday, August 26, 2007

Split Camel Case in Ruby

WikiWords are really common and sometimes a pain for indexers and other user input devices... here is a little ruby script to split them apart.

def unCamel(str)
if str.nil?
return ""
str.gsub!(/([A-Z]+|[A-Z][a-z])/) {|x| ' ' + x }
str.gsub!(/[A-Z][a-z]+/) {|x| ' ' + x }
return str

=> " Setting Up RSS Crazy"

unCamel("IReallyHATEWikiWords").split(' ')
=> ["I", "Really", "HATE", "Wiki", "Words"]

Mongrel register_proc

Added a register_proc function to mongrel

require 'mongrel'

class MergeResponse
attr_accessor :out, :head
def initialize(head,out)
@head = head
@out = out

class ProcHandler < Mongrel::HttpHandler
attr_accessor :block
def process(request, response)
response.start(200) do |head,out|,,out))

class Mongrel::HttpServer
def register_proc(str, &proc)
hand =
hand.block = proc

h ="", "3000")
h.register_proc('/justin') {|req, res| res.out.write('smile') }

Sunday, August 19, 2007

RAD Ruby for Microcontroller

Just found an AWESOME project Ruby RAD! I always dreamed of writing all my embedded programs for a microcontroller in ruby instead of C. There is so much highlevel languages can do in the embedded space I just never had time or the skill to implement it.

Demo Video of Ruby for Microcontrollers

class MySketch < ArduinoSketch
output_pin 7, :as => :led
def loop
blink led, 500

code stolen from an Informative RAD blog post

Ruby's Hash has a default value

FYI: Ruby's Hash object has a default value :-) which just makes code even more elegant

h = # default = 0
=> {}
h['justin'] +=10
=> 10

or even better:

@index =
@index['justin']['cool'] += 20

Ruby Database .... close ruby CDB

I've always want a plain simple ruby database. I've been searching for a lightweight solution to this "desire" I won't even call it a need.

I tried ruby-cdb

basically its a ruby wrapper for access to the "constant Database"
-fast reads
-big data
-good writes

I currently have an error when I try to run it, but I think it was a permissions problem on my linux machine.

undefined method `create' for CDB:Class

Any ideas?

Example Code:

require 'cdb'

CDB.create('cdb', 'tmp', 0600) do |data|
data['eins'] = 'ichi'
data['zwei'] = 'ni'
data['drei'] = 'san'
data['vier'] = 'yon'

print CDB.dump('cdb'), "\n"

CDB.update('cdb', 'tmp') do |read, write|
write['one'] = read['eins']
write['two'] = read['zwei']
write['three'] = read['drei']
write['four'] = read['vier']

Piping to Ruby

Piping to Ruby to do processing

cat junk.txt | ruby -ne "print $_"

or something cooler

cat junk.txt | ruby -ne "print $_ if $_.size < 20"

Ruby is a programmers swiss army knife.

Saved us from having to either learn regex or write awk :-)

Webrick + Erb = Simple Clean Front End

Use webrick to create a front end for you ruby application. I found embedding ErB (rails like templating) makes ruby ui lightweight and easy.

require 'webrick'
include WEBrick
require 'erb'

s = :Port => 80,:DocumentRoot => Dir::pwd + "/web_docs" )

class SearchServlet < HTTPServlet::AbstractServlet
def do_GET(req, response)'web_docs/search.rhtml','r') do |f|
@template =
response.body = @template.result(binding)
response['Content-Type'] = "text/html"


s.mount("/search", SearchServlet)


Blog realization

I now first realized why programmers should blog... So that someone else doesn't have to go through the pain you just did :-)

Time for me to bandage the wounds. I'm still bleeding. :-) let the show begin.