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 = DBM.new('data/test')
db['hello'] = "Justin is really happy he found this"
db['super'] = ":-)"

** data saved to disk directly

db = DBM.new('data/test')
db['hello']
=> "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 ""
end
str.gsub!(/([A-Z]+|[A-Z][a-z])/) {|x| ' ' + x }
str.gsub!(/[A-Z][a-z]+/) {|x| ' ' + x }
return str
end

unCamel("SettingUpRSSCrazy")
=> " 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
end
end

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

class Mongrel::HttpServer
def register_proc(str, &proc)
hand = ProcHandler.new
hand.block = proc
self.register(str,hand)
end
end

h = Mongrel::HttpServer.new("0.0.0.0", "3000")
h.register("/files", Mongrel::DirHandler.new("."))
h.register_proc('/justin') {|req, res| res.out.write('smile') }
h.run.join

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
end
end

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 = Hash.new(0) # default = 0
=> {}
h['justin'] +=10
=> 10

sweet!
or even better:

@index = Hash.new(Hash.new(0))
@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
http://raa.ruby-lang.org/project/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'
end

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']
end

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 = HTTPServer.new( :Port => 80,:DocumentRoot => Dir::pwd + "/web_docs" )

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

end
end

s.mount("/search", SearchServlet)

trap("INT"){
s.shutdown
}
s.start

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.