Monday, August 27, 2007

Erlang Bit Syntax II

Since my previous post on the subject was so lame you should take a look at a better article on the subject.

Saturday, August 11, 2007

Erlang bit syntax

I've been hacking around with Erlang off and on the last few weeks because I think it will be immediately useful for a few problems I'd like to solve. When people are first exposed to Erlang they are often impressed by its support for massive concurrency and crazy "assign once" variables. Aside from that I've found Erlang's bit syntax to be a pleasant surprise.

What would someone want to do with a bunch of nasty bits? Well the example in the PragProg book shows how to create a SHOUTcast server for streaming MP3s. Using the bit syntax you can elegantly chop up the binary blobs to read the metadata.

Here's an (unrelated) example taken from erlange.se:

DgramSize = size(Dgram),
case Dgram of
<<?IP_VERSION:4, HLen:4, SrvcType:8, TotLen:16,
ID:16, Flgs:3, FragOff:13,
TTL:8, Proto:8, HdrChkSum:16,
SrcIP:32,
DestIP:32, RestDgram/binary>> when HLen >= 5, 4*HLen =< DgramSize ->
OptsLen = 4*(HLen - ?IP_MIN_HDR_LEN),
<<Opts:OptsLen/binary,Data/binary>> = RestDgram,
...
end.



I appologize for the lack of indentation. The method I have used previously for escaping code didn't like Erlang's use of '<<' instead of blogger's PRE tag. It's that specific syntax, however, that is used for Erlang's bit packing and unpacking. Equivalent code written in most languages would get a little nasty. If you would like to see documentation on the syntax be sure to check out this site.

Thursday, June 14, 2007

Ruby closure shootout

Ok, so I too have been bitten by the arity differences between Proc.new and lambda so a few months ago I tried to find the Ruby spec to get a better understand. It was then I came to realize that the only true Ruby spec is defined by the C implementation. While such a spec may allow for rapid improvements to the language it comes at the cost of warts--and Ruby is not without it's fair share. I stumbled across this site today that is possibly the best 'research' compiled on Ruby's closures that I have seen to date. It does a great job of exposing Ruby's warts while at the same time demonstrating the power of closures in Ruby.

The article (it's really just a script you can feed the interpreter!) was encouraging in that it showed me I really should spend a little time in the MRI perusing the source for these sorts of tidbits. I can really appreciate all the work that the groups heading up the various Ruby implementations are doing to formalize a Ruby spec.

Saturday, June 2, 2007

Black-boxing Google Translate

It seems like the more I watch the Mountain West sessions the more I stumble across solutions to itches I've had recently. Just yesterday at work I was hacking with a friend on a simple CLI for Google Translate and we were struggling a bit getting 'curb' to do exactly what we wanted. After watching James Britt's session on Black-boxing with Ruby I realized that mechanize would probably be a better choice for what we were trying to do. 10 Minutes later:

require 'rubygems'
require 'mechanize'

agent = WWW::Mechanize.new
agent.user_agent_alias = 'Linux Mozilla'
page = agent.get("http://translate.google.com/translate_t?langpair=en|#{ARGV.shift}")
translate_form = page.forms[0]
translate_form.text = ARGV.join(' ')
translate_form.ie = "UTF8"
translate_form.hl = "en"
results = agent.submit(translate_form)
puts Hpricot(results.body).search("//div[@id='result_box']")[0].to_plain_text


# Examples
ruby translate.rb zh-CN "I love to eat tacos on Tuesdays" => 我爱吃tacos周二
ruby translate.rb es "I love to eat tacos on Tuesdays" => Amo comer el tacos el martes

While these may not be accurate translations, this script definitely works for testing out i18n functionality until the real translations come in. If and whenever Google decides to release an API for their site it will be painless to throw this script away.

Programming Rule #42

"Every problem has already been solved. It is Open Source. And it is the first result on Google"
--Ara T. Howard (I don't know if he's actually the one who said that first, someone correct me if I'm wrong)

I spent some time this weekend watching the Mountain West Ruby Conf's videos and I must say what I've seen so far has been great (now I'm really exited about the Ruby Hoedown). The session on Ruby Queue is going to be immediately useful to me in my effort to distribute selenium tests over a linux cluster. It's actually quite amazing that the solution I had cooked up approached the task of clustering from almost the same way--simply using a queue to feed shell tasks to an array of nodes. It's quite ironic in light of Ara's quote on Rule #42 and his project that basically sent mine to the grave.

Luckily my solution was only the product of a few weeks of work off and on. In < style="font-weight: bold;">it's that simple. So, I'm going to take my project off rubyforge and stick it somewhere else. The one thing that I really need that RQ doesn't handle is clustering with Windows machines. Since I need to run tests with IE I'll have to spend some time to see if this is something that could be worked into RQ. I have absolutely zero experience with Windows and NFS so I don't have any idea how much work would be needed at this point.

Tuesday, May 8, 2007

": No such file or directory" error from bash

I hit a strange problem this morning that I hadn't seen before. I tried to run a ruby script and bash kept telling me ': No such file or directory'. I then tried feeding the script to the interpreter directly and things worked as expected. After double and triple checking my shebang line everything seemed correct. It then occurred to me to think about where I had gotten the particular script in question from. It was generated by the Selenium IDE while I was on a windows test machine a few weeks before. As many already know line breaks in the windows world end with simply a carriage return and not a carriage return linefeed pair. I used the wonderful dos2unix utility to sanitize my script and then everything worked as expected.

Friday, April 27, 2007

Get to know your inner eigenclass

I spent some more time looking for interesting articles on ruby metaprogramming. After deciding upon my method of search I quickly fired off an amazing google query for (hold your breath...) "ruby metaprogramming". I came across this article in no time and when I saw how horrible the site looked I quickly closed the page and begged my eyes for forgiveness. A little while later I came across another blog that was praising that particular article so I figured I should at least read a bit. It turned out to be a very well written article that helped me understand the concept of class vs. metaclass (aka eigenclass). I recommend viewing that page in elinks.

* Observant readers will note that the theme used in the aforementioned site is the exaxt same as the author of this blog. It's now become patent to me how hard it is to read. You should probably be reading this site in a feed reader anyway.

Tags

my google reader feed