Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Tuesday, February 8, 2011

The type casting edge case in Rails validation

Validation is one of the things Rails does very well. Most of the time you can manage your validation with one of the standard declarative validations. Occasionally you need something custom, but even then life is good and your custom validation is as simple as writing a two or three line method. However, there are edge cases and I'm going to talk about one. Let's start with the request. Request parameters come in to your application as strings and rails automatically maps them to the type prescribed by your DB.

If you aren't thinking about this (and it is easy not to think about it) you can be in for some surprises. If your field is an integer, but a user supplies a non numeric string, it will get type cast as zero. If your field is a date, but a user supplies an invalid date, like February 31, you will get March 3rd. If they were really off and supplied February 43rd, you will just get nil.

This leads to some confusing behavior. Imagine you have an optional date of birth field on a form and a manual validation to ensure that date is in the past. If somebody enters February 43rd 1969 as their date because of type casting the field is set to nil and nothing will happen. Which is weird.

If we want to give our users meaningful feedback about their dates we are going to have to do something about this. Looking at stackoverflow. Most people trying to solve this problem just override the accessor. Bad! Changing the behavior of an accessor in the best case is introducing unexpected behavior to your models and in the worst case will cause bugs because that isn't how a standard activerecord object works.

The Rails folks have already considered this problem and so there is a feature that works around it. It seems not too many people know about it, but it is documented! ActiveRecord has alternate versions of every attribute. They are aptly named *_before_type_cast. So in our case date_of_birth_before_type_cast would contain the string 2/43/1969. And you can use that value to give a more accurate error message. I lost a couple hours the other day researching this problem, but lesson learned and I thought I would spread the good word about _before_type_cast.

Wednesday, June 23, 2010

Testing image generation with Cucumber and Sikuli


Image recognition can be a useful weapon in your testing arsenal. Our current project is a rails app that generates advertisements. We use sikuli's image recognition to detect that the text and images that we expected to be in the advertisement were actually in the ad. To integrate sikuli with our build we simply created a cucumber step that calls sikuli. Let's cut to the chase and see a sample test.

Scenario: Dealer places a Campaign Ad
Given "Bill Murray"'s dealership "Ghostbusters" exists in the system
When I log in as "Bill Murray"
And I book a "Year End" campaign ad
Then the ad proof will look like "ghostbusters_tag.png"
Cool. The magic sikuli step is the one that looks at the ad proof. Here is the step implementation.
Then /^the ad proof will look like "([^\"]*)"$/ do |filename|
feature_dir = File.expand_path(File.dirname(__FILE__) + '/../../features')
sikuli_script_dir = "#{feature_dir}/sikuli"
verification_image = "#{feature_dir}/images/#{filename}"
result = `/Applications/Sikuli-IDE.app/Contents/MacOS/JavaApplicationStub #{sikuli_script_dir}/verify_existance.skl '#{verification_image}'`
result.should match(/hallelujah/)
end
Pretty straightforward. Call sikuli and pass it a sikuli script and the image. The sikuli script returns the word 'hallelujah' if it looks at your app and determines that the verification image exists anywhere on the screen. Ok. Lets drill down into what the sikuli script looks like. Get your Python goggles on...
import sys
image = sys.argv[1]
switchApp("Firefox.app")
type(Key.DOWN, KEY_CMD)
if exists(Pattern(image).similar(0.91)):
print "hallelujah"
else:
print "bugger"
This one is brief, but I'll explain a bit. Sikuli is a cross platform tool for scripting your computer as well as interacting via image recognition. In this script we make sure that firefox is the app in focus. The image we care about is at the bottom of the page so command down tells firefox to go to the bottom of the page. Finally it looks for the image match. The 0.91 number is basically telling sikuli to be damn sure. We print hallelujah if there is a match.

Ok. That is what I am doing, but in practice how does it work? Well, it has actually been pretty trouble free, and it even finds bugs and problems. Amazing! However it isn't perfect. Here is my long list of things that could be improved:
  • Sikuli's documentation on how to run sikuli from the command line is out of date.

  • You have to compile the python down to an skl file if you want to run from the command line. Annoying.

  • Use sikuli IDE's screenshot mechanism. Other screenshot tools cause trouble.

  • Set the similarity number high to prevent false positives. For example at 0.85 similarity, sikuli couldn't tell the difference between the word Ghostbusters and Ohostbusters.

  • Creating the screenshots for tests is tedious

  • My sikuli script is naive, which means if you have multiple firefox windows open it could fail erroneously

  • A popup warning or some always on top window could cause the test to fail erroneously

  • If the browser scales the image your test will fail erroneously
This has been indespensible for our application, because it lets us know if we have broken the integration with InDesign. It also gives us a way to test that our InDesign server is behaving. Image recognition is a bit of an edge case when it comes to testing web applications, but it is an interesting tool for testing and one that is highly adaptable. For images generation applications including charts or custom javascript controls I think image recognition would be the only way to test. It would be super cool to have an image recognition gem because then we would be able to do these kinds of tests without calling out to sikuli.



Just for fun I included this last screenshot. It is sikuli IDE's nifty little image recognition troubleshooting tool. Here I have turned down the similarity number to 0.5 and sikuli is showing 3 matches for my test image of the cucumber on the page. The bright red match is the strongest and the pink and purple are weaker.

Monday, June 21, 2010

Lines of code vs. Lines of test

My last project was a rescue mission. A 20k line rails app with only about 34% line coverage from the tests. No wonder the previous team had failed. 5 months later we made a successful rescue, and were up to about 71% line coverage. Watching those stats go from totally-screwed to mostly-un-screwed felt good. Metric_fu provided all the cool and useful graphs for that project: rcov, flog, reek, etc. One graph that was missing was lines of code vs. lines of test. I was sure that graph would have made a big beautiful X marks the spot, because LOC had steadily gone down and LOT had steadily risen. The moment those lines crossed would have been a great opportunity for toasting the health of the codebase.

Well, good news for those of you looking for excuses to toast your codebase. The brand spanking new Metric_fu 1.4 features a froody new LOC vs LOT graph.
I've been using it on my new project. This codebase is healthy, so no X marks the spot action here, but it is still interesting. From our graph it looks like things started out with 2 lines of test for every line of code, and have been working their way towards 3 lines of test for every line of code. (Our rcov % has stayed about 98% the whole time.) Does that mean that as the codebase increases you have to have an increasing ratio of tests to code? Given a sampling of 2 projects I can't really make any useful generalizations. But what I can do is recommend metric_fu to everyone. Graphs are a great way to start talking about how to improve your codebase and your process, and metric_fu makes it easy to get started. And remember, always use your Vanna White hands when you are exhibiting your graphs to your team mates. If it can make a game-show as lame as Wheel of Fortune successful, then it may work for you.

Thursday, June 17, 2010

Build your views before working on the controller


For complex forms you save time by building your view before you write tests and code for the action in the controller. For example take the following view code:


- form_for :ad_request, @ad_request, :url => ad_request_feature_path do |f|
= f.error_messages :class => 'error'
.form
#publication.form-item.first
- f.fields_for :car_selections do |cs|
= cs.label :headline, "Headline:"
= cs.text_field :headline


Unless you bleed rails you probably can't guess what the params hash posted to the controller will look like. Fortunately for lazybones like me you don't have to guess. Simply create the view, run up your app in dev mode, and post this form. Of course you get an error, but if you look in your app logs you are rewarded with a real life example of your params hash, like this:
{"ad_request"=>{"car_selections_attributes"=>{"0"=>{"headline" => "Mini for $15,000 Driveaway", "id"=>"1"}}}
Great. Guess what you should do with that nugget? That's right! Use it as the starting point of your controller test:


it "should save headline" do
car_selection = Factory.create(:car_selection, :id => 1)
ad = Factory.create(:ad_request, :ad_type => 'retail', :car_selections => [car_selection])
post :feature, {"ad_request"=>{"car_selections_attributes"=>{"0"=>{"headline" => "Mini for $15,000 Driveaway", "id"=>"1"}}}
ad.car_selections[0].headline.should == "Mini for $65,000 Driveeaway"
end


This is one of those things that is almost too pedestrian to even talk about, but it can save you time and pain in the long run, especially for the occasional nasty forms with deeply nested objects.

Wednesday, May 19, 2010

Simple Tabbed Navigation in Rails


Let's say I have a webapp that can be broken into 3 broad categories of activity: Lies, Damn Lies, and Statistics. My webapp has a tab for each of these categories. When I am on the page that tells Lies I want the Lies tab brought to the foreground. When I am creating a new Damn Lie I want the Damn Lies tab highlighted, etc. This is basic webapp stuff. In the rails universe many gems have been written to simplify this, but unless you have complex requirements I call YAGNI! I worked out a simple solution for this and thought I would share.

Diving right into the code let's look at the markup that creates the tabs. I keep it in the toplevel application layout, so it appears on all pages of the application:

<% yield :selected_navigation %>
<div id="container">
<div id="tabs">
<ul>
<li class="<%= selected_navigation(:lies) %>">
<%= link_to "Lies", lies_path %>
</li>
<li class="<%= selected_navigation(:damn_lies) %>">
<%= link_to "Damn Lies", damn_lies_path %>
</li>
<li class="<%= selected_navigation(:statistics) %>">
<%= link_to "statistics", statistics_path %>
</li>
<li class="logout">
<%= link_to "Logout", "/logout" %>
</li>
</ul>
</div>
</div>

First, notice the yield for :selected_navigation. Views in the application implement :selected_navigation and use it to set a variable that maps a page to a particular tab. Here is a snippet from a page that maps to the :damn_lies top navigation element:

<% content_for :selected_navigation do%>
<% @selected_navigation = :damn_lies %>
<% end %>

The final piece of the puzzle is the selected_navigation helper that is being called for each li in the layout. It compares the current page's navigation mapping @selected_navigation to the desired tab. If it finds a match the "current" class is added to the tab. And that gives the user a visual cue as to which tab we are currently on. Here is the code for the helper:

def selected_navigation(element)
element == @selected_nav_element ? "current" : ""
end

This is a lightweight solution. I like it because it doesn't require you to put anything in the controller, or to have some annoying configuration file that maps pages or controllers to tabs. So how do you usually do this?

Wednesday, April 21, 2010

If it's soap, why do I feel so dirty?


I have an son 19 months old. He wears white converse lo-tops, each one smaller than a computer mouse. My other baby is a rails app I've just started working on. It is 8 days old today! This little rails app has reached an important milestone. It is live! Deployed! It's like a toddler taking its first steps.

Me: "The internet is a big place baby!"
It: "Gaaaaaa!"
Me: "I don't quite understand."
It: "Gaaa Gaaaa Ga!"
Me: "One second. I'll take a look at the logs... Oh Yuck! Your diaper is full of XML!"
It: *crying*


Integration. Growing pains. You've been there. My little baby integrates with Adobe Indesign CS4 server. And boy, what a piece of junk it is. If you look closely at the words "Indesign Server" you can see that "Server" has been stuck on with a wad of chewing gum and a roll of tape. Integration is done through a little soap interface that doesn't really understand soap. The ruby gem savon couldn't get a message through. I nearly lost my cool. Just as I was about to get out my blowtorch and a troop of monkey patches I had a brainwave. The API only specifies one command, "runScript". Why do I need a soap library for an API with just one command? I don't! I simply created a template from the sample soap request supplied by their instructional PDF. Then I threw in a bit of string interpolation and presto! Soap without all the horrible boiling chemicals, lye and fat. (BTW, Sorry for the lousy baby metaphor. But not for the XML thing. I really feel the same about poopy diapers as I doo about XML.)

Tuesday, April 20, 2010

Engine Yard vs Heroku: Getting Started


Today we evaluated Rails cloud hosting offerings from Heroku and Engine Yard. Each company claims you will have your app up and running in minutes. I thought I would take a moment to share our experiences with each vendor.

Here is the Heroku narrative.
Signup was simple. Just confirm your email address and you are in. Free rails hosting without a bunch of BS. And it is a good service. You can't beat Heroku for that. Deployment is simple if you run a vanilla application, but you can run into all sorts of problems if you don't. Heroku applications run in a sandbox where your filesystem is readonly (except tmp). We ran into 2 problems. First, we are using bundler. Normally you gitignore your .bundler directory in your application, but because of the heroku readonly filesystem you have to commit your .bundler directory so when heroku runs bundle install there is a environment.rb file already in place. Another gotcha is that we use sass, which compiles to css. You have to check in the compiled css files for the same reason. There is a gem called hassle (hilarious) that has been created to get around this problem. Finally, we got the app up and running on Heroku. I still give heroku high marks becuase of the free service and the possibility of a simple deployment. My Heroku pro tip: If you plan to go Heroku, deploy the moment you create the skeleton rails application and make deployment a nightly step in your CI server. That way when you introduce some dependency that Heroku hates, you know immediately.

Now for the Engine Yard narriative. Signup is again pretty easy with EY, but you have to have a credit card to do anything because creating an app instance costs 11 cents an hour. Move along freeloaders. No free rides at Engine Yard! Deployment is very customizeable, yet simple. Give them the URL to yout git repo and click "deploy". EY pulls your app and does the rest. While using bundler at Heroku was major ass pain, with EY it is a big benefit becuase you don't have to manually specify the gems on your server. Simply install the bundler gem. Then create a file called deploy/before_migrate.rb in your application. In that file you can call "bundle install", and you are all done! The beauty of EY is that you can use a fancy web interface to do nearly everything, or you can drop down to SSH and hack around at the command line. So though it looks harder because there are more levers and knobs, in the end deployment at Engine Yard is easier than Heroku because the environment is alot like your development environment. There are no magic readonly filesystem or special remote rake tasks. If I want to see what's up with the app I can just tail a log file or run script/console. Overall high marks for EY's customizeability, and ease of deployment. My EY pro tip: go look at "chef" the deployment tool that EY uses on github. If your app has any custom tasks that you need to run before it starts up you can specify them there.

What about cost?
Heroku offers a free service, which is a totally awesome resource for the Rails community. Hats off to Heroku for that. On the downside, with Heroku everything is an add on. 20$ a month to send emails, 100$ a month for SSL, 200$ a month for a dedicated DB. That adds up! EY's cheapest option runs about 70$ a month. A dedicated DB system costs 170$ a month, but there aren't lots of add on hidden costs. The big kicker is that with Heroku you pay lots of money to run a single application. With EY you pay lots of money and get a server which means you can run as many applications on that one server as you like. I think you get more bang per buck with EY.

I know. Blah blah blah, what is the verdict? Well, sorry to dissappoint, but as usual, it depends. If you want it to be free Heroku is the only game in town. If your application is very, very simple or doesn't have any of these constraints, Heroku is probably the way to go. But, if you are willing to pay for it you get more bang for your buck with EY.

Both companies have great offerings. I'd say use both of them. The great thing about being in the cloud is that you are paying on a hourly or per cpu cycle basis. Just turn the server on when you need it. If you aren't using it, you don't pay.

Monday, April 12, 2010

Rails for Fashionistas


Today somebody asked me if I would use Rails without plugins or gem extensions. Can you believe it? I looked her up and down and gave her the triple snap. Then I asked her If I looked like the kind of programmer that would let my apps go prancing around the inter-tubes naked? Like, even on a bad day I'm going to dress up my application in a few sexy little plugins, not to mention some tastefully chosen gems. At this point she asked me for advice on which plugins and gems to use. Here was my advice:

Wanna wear something classic? Will_paginate is like the little black dress of plugins. It looks good year after year. Want something a bit more risky? Haml and Sass are hot right now, but they'll both probably go the way of the swatch watch. What's out of date? restful_authentication is like soooo rails 1.x. Another thing is that the big fashion houses are always worth watching. 37 Signals? they are like Versace, Gucci and Prada all rolled into one. DHH, OMG, you brought sexy back. Thoughtbot? Yea, they're another one you can count on for high-end ready-to-wear accessories. Like, when I think about factory_girl's Factory.stub method I go out and by my infant son a new fur coat.

So, like, you can tell our conversation was going well, but can you believe it? Some neanderthal wearing a pager interrupted us. He started talking about Struts and some kind of config file generator plugin for eclipse he was working on. Before we could stop him he emailed us the url of his CVS repository. Then he got paged and had to leave. We both looked at each other and laughed until we cried. I mean, please! Don't talk to me about your Java frameworks. I don't shop at Wal-Mart and I sure as hell won't use J-terd. Hey Ricky Martin, 1995 called and they want their animated gifs back!

That's right sweetheart, Rails is all about the bling! Sure, sometimes late at night while github is down and the home shopping network is the only thing on TV I find myself thinking about something Zed Shaw said. Then I pour out some liquor for _why the lucky stiff. I hit rock bottom and imitate Marilyn Monroe making drunk kissy faces to JFK. Camellllotttt! Where have you gone!?! But then @wycats tweets about the Rails 3 beta and I'm like, "Screw it! Rubys are a programmer's best friend!"

Wednesday, February 24, 2010

Vim vs. RubyMine

RubyMine (RM) is my favorite Ruby IDE. Vim is my editor of choice. Which one is best? I'm going to try to find out. Here are the features I will be judging on.
  1. Quickly find files
  2. Keep recently used files at my fingertips
  3. Autocomplete long variable and method names
  4. Do common VCS commands like diff and revert
  5. Supply a tree view of the project directories
  6. Jump to method definitions
  7. Text search
  8. Run Tests
  9. Debug
  10. Performance
  11. Cost
  12. Learning Curve
On its own Vim can't do many of these things, but thanks to some powerful plugins this will be interesting. You can find my entire .vim directory and my .vimrc on github

Things to note
  • I'm using the rails codebase to illustrate my examples.
  • I'm omitting installation discussion and ask you to refer to github if you need details on how things are configured.
  • My plugins have keymappings that are setup to be reminiscent of RM/IntelliJ
  • vividchalk is simply the best color scheme with desert256 a close second
Quickly find files

In vim I use the fuzzyfinder plugin for this. In RM there is "Go To File" (ctrl-shift-n). Both of these pretty much just work, each one has nuances that skew things in their own favor so I'm calling this one a draw. Here is a screenshot of me finding base.rb in active_record with fuzzyfinder.




Keep recently used files at my fingertips


In vim I use the MRU plugin for this. It shows you a list of most recently used files and lets you quickly jump to one. I map this plugin to ctrl-e. RM has the same functionality. Once again a tie.

Autocomplete long variable and method names


In Vim I use Supertab for this and it is an amazing tool that nearly always offers good completions. By contrast RM ctrl-space often times offers no suggestions. Here is an example. Opening up active_record/base.rb to the respond_to? method in each app, when I begin to type DynamicFinderMatch, "Dyn..." and ctrl-space in RubyMine I get no suggestions. With supertab I get 3 suggestions: Dynamic, DynamicFinderMatch and DynamicScopeMatch. Below are some screenshots of Vim and Rubymine tab completion in action. Viva Supertab!





Do common VCS commands like diff and revert

In Vim I use the vcscommand plugin for this. Mostly I do things like diff my working copy against the repository, revert files to pristine condition or look through the VCS logs. vcscommand does all these well, plus it supports more VCS systems including git, hg, and bzr, but Rubymine really shines because of it's excellent visual diff/merge tool. Winner RM. Here is a screenshot of vcscommand showing base.rb's commit history.



Supply a tree view of the project directories

In Vim I use the NERD tree plugin for this. I find nerd tree to be just as good as the project view in RubyMine, though it is more difficult to use because I can't be bothered to remember all the nifty commands and shortcuts for doing things to the filesystem via the NERD tree. Edge RM.



Take me to method definitions

In Vim I use exuberant ctags for this. This is a standard vim feature, not a plugin. Ctags builds an index of your code that vim can use to navigate files. It works ok for standard ruby, but with lots of metaprogramming (rails anyone?) it really sucks. RM command+click offers much better support for this. Caveat, I think rails.vim helps with this and lets you jump to related files (related controllers and views or models and migrations), but for jump to method and variable definitions RM is king.

Text search

You can do inline grepping in vim with grep.vim, but I usually just grep from the command line. Once again this is an area where RubyMine has the edge. RM's collapsable search results that you can click through are powerful.

Run Tests

You can invoke tests from either one (though I don't have it setup in vim because I just use autospec which constantly runs my tests in the background). Still RM has pretty green bars and I'm a sucker for that. Edge RM.

Debugging

Vim has a Ruby Debugger called vim-ruby-debugger. Doing TDD I rarely find myself in need of a debugger so I haven't played with it. From the description and screenshots it looks as good as the RubyMine offering, so I'll give it the benefit of the doubt here and say tie or even who cares.

Performance

RubyMine gobbles up memory. 30 minutes of editing the rails codebase and it is using 703 MB of RAM! Vim is using 11 MB of RAM. RubyMine is slow to startup, and has occasional performance hiccups too. Vim wins.

Cost


RubyMine costs 100$ for a commercial license. Vim is open source. Good Karma. Vim wins.

Learning Curve


Vim requires you to know all sorts of arcane commands and to be really productive you need lots of plugins. So the learning curve is quite steep. RM has a much more intuitive interface and is ready to go out of the box. Edge RM

Conclusion

Ok, RM wins but with an asterisk. RM has the advantage of being completely focused on Ruby, HTML and JavaScript. It does one thing and does it well. Vim on the other hand will happily highlight and edit nearly every programming language or filetype known to man (or machine). 20 years down the road when Rails is a dinosaur, the new COBOL, and you have moved on to working in language X, your RubyMine skills will be irrelevant while the Vim you picked up might still be serving you well... But yea, most people will probably be slightly more productive in RubyMine. So what do you do? I don't know, so I say just learn them both and use them both.

Sunday, November 1, 2009

Macports voodoo distortion field


Today I was building the opensprints-core gem. The Rakefile depends on Jeweler which depends on the Git ruby gem. No problem. I got all that flotsam and jetsam installed and wouldn't you know my version of git was too old. Shaving yaks is dangerous work. I broke out into a cold sweat because I knew git was installed via macports and macports works by rube-goldberg-inter-dimensional-voodoo-distortion-field, 800 rolls of duct tape and a buttplug. Let's just say I've had some bad experiences with it. On top of that I haven't tried updating anything ports related for a year or two. So let's upgrade.

sudo port selfupdate
sudo port upgrade outdated

Sure enough, I start upgrading and BOOM. Some obscure library fails to build.

---> Building cyrus-sasl2
Error: Target org.macports.build returned: shell command " cd "/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_ports_security_cyrus-sasl2/work/cyrus-sasl-2.1.23" && /usr/bin/make -j1 all " returned error 2
Command output: /usr/bin/make all-recursive
Making all in include
make[2]: Nothing to be done for `all'.
Making all in sasldb
ar cru .libs/libsasldb.a db_ndbm.o allockey.o
Making all in plugins
make[2]: Nothing to be done for `all'.
Making all in lib
make[2]: Nothing to be done for `all'.
Making all in utils
/bin/sh ../libtool --mode=link /usr/bin/gcc-4.0 -Wall -W -O2 -arch i386 -L/opt/local/lib -Wl,-rpath,/opt/local/lib -L/opt/local/lib -o pluginviewer pluginviewer.o ../lib/libsasl2.la -lresolv -lresolv -lpam
/usr/bin/gcc-4.0 -Wall -W -O2 -arch i386 -L/opt/local/lib -Wl,-rpath,/opt/local/lib -L/opt/local/lib -o .libs/pluginviewer pluginviewer.o -L../lib/.libs -lsasl2 -ldl -lresolv -lresolv -lpam -lresolv -lresolv -lpam
Undefined symbols:
"_auxprop_plugin_info", referenced from:
_main in pluginviewer.o
_main in pluginviewer.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make[2]: *** [pluginviewer] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2
Luckily the Google knew how to fix it.
{{{
sudo port deactivate cyrus-sasl2
sudo port clean cyrus-sasl2
sudo port upgrade cyrus-sasl2
}}}
Then I hit the wall

---> Building MPlayer
Error: Target org.macports.build returned: shell command " cd "/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_ports_multimedia_MPlayer/work/MPlayer-1.0rc2" && /usr/bin/make -j1 all " returned error 2
Command output: /usr/bin/gcc-4.0 -I../libavcodec -I../libavformat -Wdisabled-optimization -Wno-pointer-sign -Wdeclaration-after-statement -I. -I.. -I../libavutil -Wall -Wno-switch -Wpointer-arith -Wredundant-decls -O4 -march=i386 -mtune=i386 -pipe -ffast-math -fomit-frame-pointer -mdynamic-no-pic -falign-loops=16 -DSYS_DARWIN -shared-libgcc -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -DHAVE_CONFIG_H -I/opt/local/include/lzo -I/opt/local/include -I/usr/local/include -c -o ao_mpegpes.o ao_mpegpes.c
/usr/bin/gcc-4.0 -I../libavcodec -I../libavformat -Wdisabled-optimization -Wno-pointer-sign -Wdeclaration-after-statement -I. -I.. -I../libavutil -Wall -Wno-switch -Wpointer-arith -Wredundant-decls -O4 -march=i386 -mtune=i386 -pipe -ffast-math -fomit-frame-pointer -mdynamic-no-pic -falign-loops=16 -DSYS_DARWIN -shared-libgcc -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -DHAVE_CONFIG_H -I/opt/local/include/lzo -I/opt/local/include -I/usr/local/include -c -o ao_null.o ao_null.c
/usr/bin/gcc-4.0 -I../libavcodec -I../libavformat -Wdisabled-optimization -Wno-pointer-sign -Wdeclaration-after-statement -I. -I.. -I../libavutil -Wall -Wno-switch -Wpointer-arith -Wredundant-decls -O4 -march=i386 -mtune=i386 -pipe -ffast-math -fomit-frame-pointer -mdynamic-no-pic -falign-loops=16 -DSYS_DARWIN -shared-libgcc -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -DHAVE_CONFIG_H -I/opt/local/include/lzo -I/opt/local/include -I/usr/local/include -c -o ao_pcm.o ao_pcm.c
/usr/bin/gcc-4.0 -I../libavcodec -I../libavformat -Wdisabled-optimization -Wno-pointer-sign -Wdeclaration-after-statement -I. -I.. -I../libavutil -Wall -Wno-switch -Wpointer-arith -Wredundant-decls -O4 -march=i386 -mtune=i386 -pipe -ffast-math -fomit-frame-pointer -mdynamic-no-pic -falign-loops=16 -DSYS_DARWIN -shared-libgcc -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -DHAVE_CONFIG_H -I/opt/local/include/lzo -I/opt/local/include -I/usr/local/include -c -o ao_openal.o ao_openal.c
ar r libao2.a audio_out.o ao_mpegpes.o ao_null.o ao_pcm.o ao_openal.o
ar: creating archive libao2.a
ranlib libao2.a
/usr/bin/make -C input
/usr/bin/gcc-4.0 -I../libavcodec -I../libavformat -Wdisabled-optimization -Wno-pointer-sign -Wdeclaration-after-statement -I. -I.. -I../libavutil -Wall -Wno-switch -Wpointer-arith -Wredundant-decls -O4 -march=i386 -mtune=i386 -pipe -ffast-math -fomit-frame-pointer -mdynamic-no-pic -falign-loops=16 -DSYS_DARWIN -shared-libgcc -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -DHAVE_CONFIG_H -I/opt/local/include/lzo -I/opt/local/include -I/usr/local/include -c -o input.o input.c
/usr/bin/gcc-4.0 -I../libavcodec -I../libavformat -Wdisabled-optimization -Wno-pointer-sign -Wdeclaration-after-statement -I. -I.. -I../libavutil -Wall -Wno-switch -Wpointer-arith -Wredundant-decls -O4 -march=i386 -mtune=i386 -pipe -ffast-math -fomit-frame-pointer -mdynamic-no-pic -falign-loops=16 -DSYS_DARWIN -shared-libgcc -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -DHAVE_CONFIG_H -I/opt/local/include/lzo -I/opt/local/include -I/usr/local/include -c -o ar.o ar.c
In file included from /System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework/Headers/MachineExceptions.h:29,
from /System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework/Headers/DriverServices.h:32,
from /System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework/Headers/CarbonCore.h:125,
from /System/Library/Frameworks/CoreServices.framework/Frameworks/AE.framework/Headers/AE.h:20,
from /System/Library/Frameworks/CoreServices.framework/Headers/CoreServices.h:21,
from /System/Library/Frameworks/Carbon.framework/Headers/Carbon.h:20,
from ar.c:27:
/usr/lib/gcc/i686-apple-darwin9/4.0.1/include/xmmintrin.h:35:3: error: #error "SSE instruction set not enabled"
In file included from /System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework/Headers/DriverServices.h:32,
from /System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework/Headers/CarbonCore.h:125,
from /System/Library/Frameworks/CoreServices.framework/Frameworks/AE.framework/Headers/AE.h:20,
from /System/Library/Frameworks/CoreServices.framework/Headers/CoreServices.h:21,
from /System/Library/Frameworks/Carbon.framework/Headers/Carbon.h:20,
from ar.c:27:
/System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework/Headers/MachineExceptions.h:216: error: syntax error before '__m128'
/System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework/Headers/MachineExceptions.h:218: error: syntax error before 'sd'
/System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework/Headers/MachineExceptions.h:220: error: syntax error before '}' token
/System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework/Headers/MachineExceptions.h:264: error: array type has incomplete element type
make[1]: *** [ar.o] Error 1
make: *** [input/libinput.a] Error 2

Error: Unable to upgrade port: 1

The good news is git got upgraded. Mplayer and who knows what else is not. Oh well, while I am being a hater I might as well mention that the real suckiness is the fact that Leopard doesn't have a package manager. And that IS the dumbest thing about OSX.

Sunday, September 13, 2009

Ruby is a Bloodsport


8th Light's 2009 ruby sparring tournament was posted at the end of August, but I just heard about it last week. I followed the simple instructions for getting started and was quickly rewarded with a very cool limelight app that drives the code you write. The coolest part of the app is that it analyzes your player and assigns a score. The score is based on efficiency, lines of code, test coverage, various metrics and of course how well your player does at hangman. I was immediately sucked in to the fun and challenge of the problem and I have created and refined my own player and submitted it for entry in the tournament. I thought I would share a few of the things I learned that might help others improve the scores of their own hangman players.
  1. "Simplicity Score" depends on lines of code, including comments, so delete all comments from the code to maximize your simplicity score.
  2. "Flog Score" is an easy one to maximize. Run flog separately on your code. Anything with a flog score above 9 will cost you points. Refactor bigger methods to smaller ones to keep their flog score below the limit.
  3. "Time Score" can be tricky to maximize. One approach is to create a performance test to help drive out the bottlenecks in your code. The hangman uses a list of 10,000 words so any time you iterate over that complete list you kick holes in performance.
Those were the 3 biggest things that helped me get my player score up there. It was definitely lots of fun and a good way to sharpen skills. I'm looking forward to seeing how the tournament plays out. Drop me a line if you wanna talk shop on this. Hope you decide to join in. And a big thank you to the folks at 8th light for making this happen.

Saturday, August 8, 2009

Anonymous Inner Classes: A Poor Man's Closure?


Yesterday I had a discussion with a colleague about whether Java has closures or not. We both could immediately agree that, formally speaking, Java does not have them, but the part of the discussion that went on and on was about passing anonymous inner classes vs. passing around an anonymous function. I argued that the anonymous inner classes were a very poor substitute because a class has so much extra baggage, both conceptually and behaviorally. Classes understand and do all sorts of things that you don't care about when you are simply trying to pass along a little ol' function. Stuff like: inheritance, encapsulation, polymorphism. Worst of all, classes require lots of messy syntax. My colleague made an important point though: What else is a Java developer gonna do? When you are working in Java and you need something like a closure you've got no choice but to go for the anonymous inner class, the poor man's closure.

Google's Collection Framework is the best example I know of Java programmers making do with their poor man's closures. Here is a quick bit of sample code I created that demonstrates applying a function to every element of a list (Sorry you can't see my generics. I can't seem to escape properly for html):


import com.google.common.collect.Lists;
import com.google.common.base.Function;
import java.util.ArrayList;
import java.util.List;
public class PoorMansClosure{
public static void main(String[] args){
//Demonstrate something similar to the map function on a collection in ruby
ArrayList strings = Lists.newArrayList("feeling", "bad");
Function upcase = new Function(){
public String apply(String from){
return from.toUpperCase();
}
};
List upperCaseStrings = Lists.transform(strings, upcase);
for(String val : upperCaseStrings){
System.out.println(val);
}
}
}

Martin Fowler has a nice article about how closures naturally compliment collections. Here is a quick bit of ruby to demonstrate that.

class Closure
def demonstrate
strings = ["feeling", "good"]
upper_case_strings = strings.map{|elem| elem.upcase }
p upper_case_strings
end
end
Closure.new.demonstrate


In this example the closure frees us from the burden of specifying the names and signatures for both class and method and we simply give the function that maps to upper case without the noise. There have been rumblings in the Java world for some time that Java might someday support closures. Here's hoping that Java 7 will. If that does happen it will be interesting to see what they do to the collection framework. The existance of Google's collection framework is proof that the standard Java framework has major drawbacks. I think a major rewrite will be in order.

Wednesday, June 17, 2009

Meta Programming with a Barf Bag


Day one of the Open Source Bridge conference was action packed. I'll probably have to write a few posts, but I'm going to start things out with notes on my favorite talk of the day "Spindle, Mutilate and Metaprogram: How far can you push it before there be dragons?" by Markus Roberts and Matt Youell. Yes the picture is them wearing colanders on their heads.

This talk was 2 hours long so it is hard to cover it in all its glory. The crux is that programmers are generally very inhibited by the orthodoxy of each particular language community to the detriment of the code they are writing. Done properly, applying the magic of meta programming simplifies problems and makes code more understandable. Markus especially seemed to wipe his boots on some of the taboos. They mentioned ActiveRecord and jQuery as examples of effective meta magic.

The talk began with a gameshow where 3 volunteers were shown code snippets and the first person who could identify what language it was got a point. The first program looked like C but was in fact Ruby. Another looked like assembly language but was Perl, they even threw some Cobol in there, but it turns out it really was Cobol. :) Each time after they revealed what the real programming language was they showed the code that allowed them to mimic the other languages. It was really a cool way to start a presentation about meta programming.

At one point they showed some C that had been made to look like another language (I don't remember which). Somebody from the audience pointed out a memory leak. Markus responded by pointing out a table in the back where they had barf bags laid out and recommended he get one because there was going to be some spectacularly intolerable displays of metaprogramming. There was lots to learn about how you can shape your own destiny in a programming language if you are willing to amp up your level of meta skulduggery. Tomorrow when I see Markus and Matt again I'll ask about their slide deck, because it would be fun to share some of their code examples.

Wednesday, June 3, 2009

To ORM or Not to ORM.


Sara Taraporella recently added this very interesting post to her blog. In this post she argues for a separation between domain models and ORM models. There is a pretty lively discussion in the comments that I found useful.

For me it is situational and so for the most part I think being somewhere between these extremes is nice. Something that keeps me from having to do lots and lots of transformations but also keeps me from having to touch too many files when something changes. Active Record is the poster child for combining ORM with the domain, and in the situation where you have complete control to define and change your DB it is great, though I think it comes at the cost of testability. In the comments Clinton Begin describes a situation where other applications need to consume the same DB and require changes. In this case or in the case of a legacy DB you might find yourself gravitating towards the opposite end of the spectrum, which is what Sara is advocating. Complete separation of these concerns.

I recently attended a talk by Drew Olson at the Chicago ruby user group about Data Mapper (among other things). Data Mapper is definitely a step away from the the purity of active record. With active record your DB schema describes your domain model and so you stay DRY. Data mapper says you need an explicit mapping. This buys you flexibility at the cost of being explicit and potentially redundant in describing the relationship between the domain and the database.

I see Active Record as one extreme and Sara's perspective as the opposite. Datamapper is a step towards the middle, and with the changes in Rails 3 the door will be open for people to use data mapper instead of active record. So it appears that Rails is mellowing in its old age, going for a less extreme worldview. Or maybe the world has moved towards rails' view of the world. In the end a little of both are a good thing, I think.

Monday, May 11, 2009

Building your resume


And when I say "building your resume" I literally mean creating a build system for a resume with config files, LaTeX and Rake. Why in the name of Knuth would I do such a thing?
  1. Because it is AWESOME!?!
  2. Version control! So how old will I be when my resume reaches 1.0?
  3. Hosting on GitHub means my resume has an RSS feed!
  4. LaTeX's Inline comments make for fun Easter eggs and color commentary.
  5. A really keen potential employer could 'watch' my resume via GitHub. Heck they could even fork my resume and add things they would like to see me learn.
  6. I could add a Rake task to publish my resume somewhere or to email it directly or even to tweet me whenever someone builds my resume.
  7. Rake task for spellcheck.
  8. I could have build parameters for adding or hiding certain sections of my resume... for example swapping out the objective at compile time.
  9. They say "blogs are the new resumes" but I say, "GitHub is the new resume!"

Monday, March 30, 2009

Searching my favorite ruby blogs


I've been making custom google search engines for things I find useful. Tonight I was doing some research on ruby metaprogramming and I decided to create a search of some of the better ruby blogs. Let me know if I am missing any in the search. In the meantime, I hope this comes in handy. Check it out!

Sunday, March 22, 2009

Google Custom Search Engines


Today I was looking a good veggie fried rice recipe. The usual delimma I face when searching the web for a recipe is that I never get the result I want. Inevitably google returns some crappy commercial site like foodtv.com or about.com or something becuase loads of people link to them. Unfortuantely, when it comes to recipes the stuff they serve up is uninspired.

What I really want is a veggie fried rice recipe from one of those artsy little food blogs like "everybody likes sandwiches" or "eggs on sunday". But how to find the artsy food blog links among all the commercial noise on google? Technology will have a solution, right? Yes. The solution is a Google Custom Search Engine. Give google a list of urls and it will let you search them exclusively.

Here is my custom food blog search:

It covers about 150 artsy little food blogs. Where did I get the URL's? I wrote a simple little ruby script to scrape urls out of HTML. While I was at it I made another search engine I thought would be useful. It is a search of every blog listed on blogs.thoughtworks.com. Here is the widget if you are interested:

Incidentially, here is the fried rice recipe that I ended up with. You gotta love the not eating out in new york blog.

Tuesday, March 3, 2009

Googling for Ruby Quiz finds an outdated site


Ruby Quiz is an awesomely fun programming challenge that comes out on the ruby-talk mailing list every week. There has even been a book of them published by the prag guys a couple years ago. Anyway, I can't stay up with the ruby-talk mailing list because there is just too much stuff flying around there. So how to get your hands on all the quiz loveliness? You could find all the quizzes in the archives of the mailing list if you had some time on your hands. Fortunately there are some websites that collect them. Unfortunately, no website (that I know) collects ALL the ruby quizzes. There are three websites though that, put together, contain all the quizzes. Old skool ruby quiz site has quizzes 1 through 156. Mid skool ruby quiz site has quizzes 157 through 188. The new skool rubyquiz site has 157 - 194. Why do they do it like this? I don't know! It is really annoying! If you google for ruby quiz the old skool site is the only one that shows up in your search. I just thought I would link to the new skool site from my blog in the hopes that someday when I search for ruby quiz I'll get a link to the site that has the latest quizzes.

By the way the newest ruby quiz "Polyrhythm Trainer" #194 sounds like a really fun problem to solve. If I had time to do it I'd make my solution a shoes app. Whee!

Tuesday, December 2, 2008

A history of the security at the Aon Center


The AON security ninjas... Should I start from the beginning? Yes? Ok. It is a looong story about ancient Japan. Two warring feudal lords loved the same woman, Iwasaki. Both lords tried to win Iwasaki over with the traditional Japanese gift of love: wooden clogs inlaid with a jade Hello Kitty. Then, on the day Iwasaki was to choose which lord to marry she died tragically of a hangnail caused by poorly fitted clogs. Each of the lords blamed the other for her death and the bitter conflict that ensued instigated centuries of assassination and espionage. Long after both sides of the conflict were all killed the clans of ninjas that were involved, known as the Iwasaki ninjas, swore their allegiance to Iwasaki's family. The family became wealthy and powerful, but during the 19th century the head of the Iwasaki family lost an epic game of flip cup to a young John D. Rockefeller. The dishonor was so great that afterwords the allegiance of the Iwasaki ninjas transferred to Rockefeller. The ninjas were the secret of Rockefeller's success and they lived in his mansion until Rockefeller's death. Suddenly in need of a place to live, and being preferred shareholders in Standard Oil, they took over the newly constructed Standard Oil building in Chicago. There they assumed responsibility for building security.

Forty years later their only allegiance is to the building and their union, the Local Ninjitsu 393. If you visit, pay attention to the dozens of large gongs that line the walls. Each gong represents a generation of Iwasaki ninjas. DO NOT TOUCH THE GONGS! A visiting member of the Chicago Ruby User Group named Josh once rang a gong. Later that evening he swallowed his own tongue under mysterious circumstances while giving a talk about metaprogramming in ActiveRecord. Detectives later found that one of his note cards had been altered to say:

I need not your needles, they're needless to me;
For kneading of noodles, 'twere needless, you see;
But did my neat knickers but need to be kneed,
I then should have need of your ninjas indeed.

The murder remains unsolved. Frankly, for most of us in the meeting that notecard was the only part of his talk that made sense, but you know, metaprogramming is hard. The guiness book of world records lists that paragraph as the most deadly tongue twister known to man. It is also worth mentioning that ever since that day the poor fellow has been known in the Ruby community as "Because, the unlucky stiff".

Saturday, November 29, 2008

Syntax Highlighting In Blogger


I have pulled various tricks in the past to get syntax highlighting in my code samples. In the past I only had highlighting for ruby code. I think I finally have a good solution and it works for any C style language.

Luka Marinko's blog has simple instructions to get this to work. Thanks Luka! Now here is some nonsense code to show what kind of highlighting you get with Java, Python, and Ruby. All the useless imports and requires are just so we can see those kinds of statements getting highlighted.
JAVA

import java.io.*;

class HelloWorld{
public static void main(String[] args) {
hello();
}

public static void hello(){
System.out.println("hello world");
}

}

RUBY

require 'rubygems'
class HelloWorld
def initialize
puts "hello world"
end
end

HelloWorld.new

PYTHON

import sys
class HelloWorld:
def __init__(self):
print "hello world"

HelloWorld()