Archive for May, 2009

Plans for Rubyyot.com and the Summer of Learning Challenge

Sunday, May 24th, 2009

Plans for the future

In the past week I started playing with Rack and put together a little Rack application that generates content from my blog’s rss feed about the site.   As I stated in that post I really didn’t like having the “parked” page up there, and I also wanted to try out Rack, which has gotten so much press of late.   I was very impressed with Rack, and how easy it was to put together a simple web application with it.

Until that point, I had used Rails, used non-Ruby web frameworks and used Ruby for other non web-based projects, but I had not created a Ruby based web application with anything other than Rails.  It was an enlightening experience.   It got me thinking about Rails, what it provides out of the box:

  • ActiveRecord for persistent, vendor agnostic storage and model building goodness
  • Built in support for AJAX and partial views.
  • Controllers to coordinate between them
  • Simple support for REST web services
  • ActionMailer for email handling
  • Flexible url routing
  • An active and growing community that provides a large number of useful and often cutting edge add-ons and guides.

Additionally, many of these features are modular and can be swapped out or replaced with other modules to suit your tastes.  That is quite an impressive and list, and by no means comprehensive.  With Rails 3 on the horizon, there are even more great things on the way.   It’s no wonder, that I fell in love with Rails.

However, I realized that a number of things I use to develop Rails applications are not truly Rails features, but only supported by Rails.  This by no means a slight against Rails, but it is something great about Ruby and the Ruby community.   Here are some features and libraries that I make use of:

  • erb – Templating engine available in ruby itself.
  • rake – An essential make utility for automating reoccurring tasks.
  • Test::Unit – Unit testing framework, baked into Ruby.
  • Cucumber, RSpec and friends – There are group of great libraries available for testing Rails and other Ruby apps.
  • git and github – Fast, distributed source control and repository hosting.
  • Rack – The new hotness in standardized web server interfaces.
  • Capistrano – Automated deployment.
  • Phusion Passenger – Once called mod_rails, it is actually more than that, as it supports all Rack based applications.
  • Dedication to best practices and self-improvement – While not technically a feature, it is ingrained in the Ruby and Rails communities and it’s contagious.

Chances are, if you have done any Rails development you have encountered, used and enjoyed some or all of these.   My point here is not to say that Rails is only great because Ruby is great.  Rather that while Rails is awesome, there is a strong and viable codebase for building applications, web-based or otherwise, in the greater Ruby community.  The relationship between Rails and Ruby and the popularity of Rails as a framework have made both better and stronger tools.

Building Cohesive Applications

This relationship would not be as be strong, if Rails was built as  a monolith.  Instead, Rails is designed in to be modular.  The framework itself is broken up into no less than 6 gems:

rubyyot@atlas:~$ jruby -S gem install rails
Successfully installed activesupport-2.3.2
Successfully installed activerecord-2.3.2
Successfully installed actionpack-2.3.2
Successfully installed actionmailer-2.3.2
Successfully installed activeresource-2.3.2
Successfully installed rails-2.3.2
6 gems installed

Within that, as we saw, the standard way of building rails uses a number of other tools and libraries.  These parts are generally built to be highly cohesive rather than highly coupled.  This property of good design is probably best described by Uncle Bob’s SOLID principles.  These are:

While these principles were written with less dynamic languages than Ruby in mind, such as Java and C#.  Understanding them and applying them  in any language will help you to write clean code.  Code with minimal dependencies that strangle it’s agility and make make maintenance a nightmare.  These SOLID principles also make code more modular and enable the long promised re-usability that all object-oriented programmers learn about in OO101.

Ruby Web Frameworks

One of the trends in the Ruby and Rails communities in the past year or two, has been the building of other web frameworks.  These were often termed micro-frameworks because they offered the ability to build ruby based web applications without having to run a full Rails stack.  The most notable of these are Merb and Sinatra.  While these began to splinter ruby based web development, my mantra for this year is:

Change is good.

These project went and approached the building of web applications in Ruby in different ways.  In doing so they opened up the doors to creativity and brought some improvements and  fresh ideas to the table.  The benefits of this are even now coming clear with the announcement that Merb will be merged into Rails 3.  This promises to bring more speed and modularity to the Rails framework.

Pragmatic Thinking and Learning

I recently completed the book Pragmatic Thinking and Learning.  It was an excellent book that centers around bringing our innate creativity (R-mode) to the party to achieve goals and solve problems.  While it’s a book targeted to technical people it is not written for one specific language or even just for programmers.  It takes a number of interesting studies and theories, most notable for me is Betty Edwards’ Drawing on the Right Side of the Brain and brings them together into a series of tips and hueristics for improving your thought processes.  It’s a very interesting book and I would recommend it to anyone interested in the topic.

Rails and Lettuce

In of the book, Andy Hunt is discussing personal wikis and shows a page from his WikiNotes (figure 8.3 ).  It shows a series of thoughts as follows:

  • U.S. per capita consumption of Romaine lettuce increased 162%
  • Lettuce consumption at a record High
  • Convenience of pre-washed, bagged lettuce outweighs cost.
  • MORAL: it’s got to be easy

I thought that this was a particularly interesting observation and in a way is a metaphor for Rails.

The sales of Romaine lettuce increased 162% by pre-washing and packaging it in convenient bags.   This packaging is widely popular in spite of the increased cost.  It’s clear that lettuce is a great product.  Everyone knows it’s healthy and it’s the building block of salads and great in sandwiches as well.  Still it’s popularity were greatly improved by giving it an instant, right out of the box, interface.   Similarly Ruby web frameworks are great products.  Rails packages that goodness up in an easy to learn, build and deploy.

Reinventing the Wheel vs. Digging a Rut

Rails is built on top of Rack.  Rack is written in Ruby and specifies the interface between the web server and the Rails framework.  In fact Merb and Sinatra are built on top of Rack as well.  Still, as we saw, Rack is available for building upon directly and of course Ruby and it’s libraries are available as well.  So why not build a Ruby web application without any framework at all?  Well the obvious answer is that you won’t have all of the nice things that these web frameworks provide.  Not to mention many of these pieces have now been used and tested by production applications for years now.  It may be a better question to ask:

Why wouldn’t I want to use one of the pre-built frameworks?

The answer to that question is what has been forming in my mind since I tried out Rack for the first time.

  • Frameworks remove options - Even the best framework on the planet is going to take away options.  Lean Methodologies include the idea of delaying decisions as long as possible allowing for greater flexability later on.  Many times we want to use a framework, because it leave a clear path to a production ready application.  This comes at the cost of options and I don’t want these removed.
  • Learning Opportunity -  Often the best way to learn something is to do it.  I’m not an expert with Rails, I still have more to learn, but I also want to learn more about writing and designing applications for the web in general.  Therefore it makes sense to bypass the framework and hook directly into Rack.
  • Many libraries are still available – As we saw before, many of the tools used in the creation of Rails applications are still available for use.
  • Completely Custom – I used to love watching shows like American Chopper and Monster Garage even though I know next to nothing about building a car or a bike.  The thrill was in watching highly skilled people create baddass and functional custom vehicles.  This is a similar though admittedly geekier endeavor.
  • Beginner’s Mind – This zen concept urges you to remove look at things without filtering them through your pre-conceived ideas and biases to gain greater understanding.  Approaching this project without relying on a pre-conceived framework should lead to similar results.

All of these are great reasons for me to try making my own path and not generating yet another Rails application.  That’s not to say that Rails isn’t great, or I’m better than Rails.  It’s just not the right fit, right now for my project.  In the end I’d rather re-invent the wheel than dig myself into a rut.

Vaporware and Playing to Learn

One thing that I do when working with my personal projects is that I will have an idea and start to run with it.  After a while of working with the idea, more ideas will come and my attention will turn away from my original project and move on to other things.  Some examples of this are:

  • Resolvr – A dependency injection mechanism for Ruby.
  • Gippr – A library for parsing the GIFT file format.
  • RSimpy – A wrapper for the API to Simpy.com

Will I ever get back to these and complete them?  Maybe.  Much of what I do in my personal projects is playing to learn.  Like when I was a kid and I wanted to build a giant working  robot or transformer out of legos.  Did I ever finish these projects?  No, but they were fun and I learned a number of interesting things along the way.  This is one of the great things about personal projects.  There is no one expecting you to deliver a product, so you are free to persue whatever interests you.  Even if that means that you abandon the project altogether and move on to another.

There is a character on the show Jimmy Neutron, that my kids watch,  called Professor Calamitous. He is a brilliant scientist bent on doing evil.  However, he is continually undone by his character flaw, that is that he can never finish anything.  I have often teased my wife, because she will get very excited about a project and work on it.  She won’t just do a little, but she really digs into it and attacks the subject.  After some time she will, almost overnight, lose interest and move on to something else.  I’ll tell her that her inner Professor Calamitous has struck.  In actuality, I’m just as guilty of it as she is if not more so (sorry for teasing you).

When viewed from a goals oriented perspective this is a horrible waste of time.  Why put all the energy into something that will be left to rot, unfinished?  From another perspective, that of a learning tool, it is a great accomplishment and a goal achieved.  The time wasn’t wasted because in doing something with the project, rather than just reading (or writing, speaking, etc.) about the topic, you develop hands on experience and engage much more of your mind (R-mode) than you would have otherwise.

Personal Wiki

Now I feel it’s my duty to bring all of these random thoughts together and issue a challenge to any out there who might be reading this.   The book, Pragmatic Thinking and Learning, recommends the use of a personal wiki to gather together your thoughts and organize them into an exocortex.  I looked through all of the available wiki software out there and none of them really felt right.  That was a kind of personal decision and it’s nothing against any of the software available.  They either didn’t provide the features I wanted, or I didn’t like the look and feel.

Requirements

  • Good feel – If I’m going to do this, I will be spending a significant amount of time with it.  Any tool I will be spending that much time with, better not have annoyances, no matter how small.
  • High Availability – While it may not need to be as available as a ubiquitous collection tool, it should be available to me at work and at home, at the very least.
  • Wiki Formatting - Whether via wiki markup, RedCloth or some other means it should be easy to build and grow as a wiki.  That’s kinda the point of a Personal Wiki
  • Single user editing -  It may seem odd to have a wiki that is editable by a single person, but if it’s just for me, then it only makes sense.
  • Public display – Some of the things I put in it would make sense to be available to others who are interested.
  • Private display – Some of the things in it may be private and kept that way.
  • Easy backup and recovery – If I’m going to go to all the trouble of writing and gardening a wiki, it should be safe and recoverable in the event of corruption, disaster or my own foolishness.
  • Customizable – I may want to have dynamic content on some pages.  Perhaps it should pull in my bookmarks on a topic and display them with other content.

That is quite a hefty list and I’m sure that there will be other things along the way that I’ll want to put in there.  It’s no wonder I couldn’t find what I wanted out of the box.

I’ve decided to build this directly on Rack, using git, github and other useful tools I would use in building a Rails app.  I’m not sure how this will go at this point, but it’s an exciting project and I hope to have something to show for it by the end of summer.

The Summer of Learning Challenge

We all have these kinds of ideas floating around in our heads, and the more creative ideas we listen to and act upon, the more will come to us.   I think that right now is a great time, as it is the beginning of Summer 2009 to act on one, or more, of these.  The summer provides a set time frame that is long enough to make something great, but short enough that we won’t put it off forever.  I think that it would be great to see others (you) find a project they are interested in a make a commitment to work on it this summer.  If you are a developer/programmer, make your project open-source.  It could be a great benefit to others.  It will defiantly be a great experience for you.

A few years ago I took part in and completed NaNoWriMo.  My book wasn’t finished at the end, but I wrote 50,000 words of it in one month.  That was a great accomplishment for me, something that I didn’t think I could do when I started.  By setting the goal and working toward it I was able to complete it.  It felt great.  What could you do if you set your mind to it?

What Will You Do?

If you are interested in taking the challenge or just have an idea or a comment about mine.  Please leave feedback or blog about it yourself an leave a postback.  It’s going to be a great summer.  Enjoy!

Introspection – May 2009

Wednesday, May 20th, 2009

Checking in

As predicted in February’s introspection, change is here and it is happening. I am embracing it as best as I can. It’s kept me busy and I haven’t posted an introspection since March. It’s more to than time to check in on my goals for the year.

Progress

  • Spend more family time – With the exception of the last week or so, I’ve been making time to spent outside with my kids. My wife and I have had more together time as well. It’s been rewarding and I’m glad I’ve been doing this.
  • Quit smokingStill smoke free As of last week I fell off the wagon. It was a good run, but the pressure got me.
  • Continue learning in Ruby and .NET – On the .NET side, I’m working on putting a second Monorail app into production, this time using Brail rather than NVelocity. As for Ruby, I’ve been actively using it and finding more and more projects to work on. Unfortunately, many of the personal projects get started and left for the next project that strikes my fancy. Still the continued use has improved my knowledge of the language and programming overall.
  • Learn Ioke – I have finally started this and once I got over my fear, I played with ISpec here and here
  • Exercise frequently – The last few weeks have killed this, but before that I was walking daily and it was great.
  • Stop trying to organize, and start simplifying – Falling apart here. I guess something’s got to give. I’ll get to this on the weekend, maybe.
  • Become financially stable – Not as far as I want to, but far better than previous years.
  • Teach – Not much progress here. Bought an interesting book called Hello World. Started KungFuTees with my kids. I’m interested in Railsbridge, maybe I’ll find some good resources here.
  • Enjoy the journey- Up and down here, some times are better than others. Working to maintain a positive perspective.
  • Blog!- Added this. I’ve been trying to post a minimum of 3 times a week. Happy about this.

Using Rack to generate content from an rss feed

Tuesday, May 19th, 2009

The Hype

Rack is taking the Ruby and Rails worlds by storm. It’s hard to find any recent Ruby related posts that don’t mention Rack in some way or other. It’s obvious that Rack is here. If you haven’t read up on it. Jason Siefer has put together a good post full of links to get you started.

The Project

I recently killed Rubyyot.com, the GTD task management site. It turns out that using computers to take notes just isn’t convenient. I haven’t quite figured out what is, but I suspect that it involves pencil and paper. In any case, I decided to take the site down since I wasn’t using it. The site had a pretty “this domain is parked” page on it, which I wasn’t particularly fond of.

So to make the site more interesting, I wanted to be able to display an news items that I might have available about the domain. However, I already have a blog, this one here. I didn’t want to write a full rails app to post blog entries, or have yet another wordpress site. What I wanted to be able to do was post to my blog and if the item had to do with the rubyyot domain have it show up in the page I have there as well.

What I needed was a way to take an rss feed from my blog for the tag rubyyot and post the items to a page on Rubyyot.com. That is exactly what I did in a few hours with some help from Rack.

The Host

Rubyyot.com is hosted on Dreamhost which supports Passenger. And here is one of the nice things about Rack. Rack is a standard interface and so Passenger supports Rack. All Passenger needs to run a Rack app is a rackup file (config.ru) a public directory for css, images and other files available to the public. Finally, it needs a file to trigger a Passenger restart, this is located at tmp/restart.txt.

It turns out that since Rails now runs on Rack, you can simply configure your app in the Dreamhost Webpanel as you would a Rails app. Instead on putting the Rails app at this location, you simply put the Rack app there. In my case I put a git repository there, though I may try to capify it in the future.

The Experience

All in all, it was a painless experience. The code needs a little cleanup but it works. Dreamhost had Rack and Hpricot already installed on the server, not the current versions of the gems, but this didn’t cause an issue. I wanted to put it out here to show how easy it can be. In the future, I would like to use a similar technique to be able to post status or news messages to any of my sites from my blog.

The Code

Note: The full code can be found on github tagged as first_post

#config.ru
require 'rack'
require 'rubyyot_status'
 
run RubyyotStatus.new

and

#rubyyot_status.rb
class RubyyotStatus
  def call(env)
    require 'rubygems'
    require 'hpricot'
    require 'open-uri'
 
    # Setup beginning of page and initial status value
    text = "<h2>Rubyyot.com is going through changes</h2>"
    text << "<p>If you are looking for my blog you can find it <a href='http://blog.rubyyot.com'>here</a><p>"
    text << "<h3>Latest Updates</h3><ul>"
 
    css = '<link rel="stylesheet" type="text/css" href="/rubyyot.css" />'
    response_body = "<html><head><title>Rubyyot</title></head>#{css}<body>#{text}"
    status = "200"
 
    begin
      # read rss feed
      doc = Hpricot.XML(open("http://blog.rubyyot.com/tag/rubyyot/feed/rss"))
 
      (doc/"item").each do |item|
	# get link, description and title from feed
	link = (item/"link").inner_html
	description = (item/"description").inner_html
	title = (item/"title").inner_html
 
	# add each post to page
	response_body << "<li><h4><a href='#{link}'>#{title}<a></h4>#{description}</li>"
      end
    rescue
      status = "500"
    end
 
    # finish off page
    response_body << "</ul></body></html>"
 
    # add headers
    headers = {}
    headers["Content-Length"] = response_body.length.to_s
    headers["Content-Type"] = "text/html"
 
    # return values
    [status, headers, response_body]
  end
end

See it in action

Working version (at least for now) is at Rubyyot.com.

Getting started with ISpec and Ioke – Part 2

Sunday, May 17th, 2009

What I’m trying to make

I was recently reading a post on using Ioke’s web kit, IKanServe, on Google’s App Engine. It comes with a built-in HtmlBuilder object, but I wanted to try making something more like a rudimentary erb. Basically a way to embed code in template files.

Evaluating on the property bag

So far I have created a basic property bag, which is really just a basic Ioke Object. At it does is serve as a portable ground on which to evaluate messages on. Next there is the Evaluator which takes a bag when it’s initialized and evaluates messages against it, returning the results.

Don’t be put off by Ioke’s advanced features, it’s quite usable

When I first thought using this as an example, this was as far as I got. I figured that If I could get to the point where I could have a parameter object that could be be evaluated against from another context, I would probably have put in a siginificant amount of work for a basic example and I could wrap it up call it a day and try using it with IKanServe. Well as it turns out it wasn’t all that difficult to do. Really it was quite simple. I updated my test/evaluator_spec.ik to read:

use("test/helper")
use("evaluator")
use("bag")
 
describe("Evaluator",
  it("should have the correct kind",
    Evaluator kind should == "Evaluator"
  )
 
  it("should have accept a bag on initialization",
    bag = Bag mimic
    bag foo = "bar"
    evaluator = Evaluator mimic (bag)
    evaluator bag foo should == "bar"
  )
 
  describe("Evaluation",
    it("should evaluate nil as an empty string",
      bag = Bag mimic
      evaluator = Evaluator mimic(bag)
      evaluator evaluate(nil) should == ""
    )
 
    it("should evaluate string on bag",
      bag = Bag mimic
      bag foo = "wuzzle"
      evaluator = Evaluator mimic (bag)
      evaluator evaluate(foo) should == "wuzzle"
    )
  )
)

Looking at it now, the last spec should read “should evaluate message on bag” but you get the idea. At this point, I set out to implement this spec.

Don’t forget commas

My biggest problem by far was of my own design, and my own fault. When I first typed up the spec I left off the comma at the end of

    it("should evaluate nil as an empty string",

This caused the compiler to output a not so pretty error message, that that took me quite some time to track down. You see, I was sure that the issue was with my implementation of Evaluator rather than a syntax error in my spec. So, if you are getting an error that says that it can’t import your spec, look for syntax errors there first. You might be missing a comma.

Principle of least surprise

One of the things I really enjoy about Ruby is that it subscribes to the principle of least surprise. That is, if you don’t know the syntax for something, there is a good chance you will find it by trying the syntax that would make sense to you. I think that this is a side effect (or not so side-effecty) of what Ola Bini recently termed Syntax over Explicit APIs in the Ioke Philosophy.

In any case, the code to pass the spec was simply:

Evaluator = Origin mimic
 
Evaluator initialize = method (
  "Pass the property bag into the evaluator, this will be used for future evaluations.",
  bag,
  @bag = bag
)
 
Evaluator evaluate = macro (
  "Takes the supplied message and evaluates it on the property bag. If nil is passed, it will return an empty string",
 
  msg = call arguments first
  msg evaluateOn(@bag)
 )
)
Bag = Origin mimic
 
Bag nil = ""

I figure that there is probably a pretty big hole in my code here somewhere that I’m going to expose as I play with this more, but for now, I’m happy that it’s passing my spec.

Update: Thanks to Sam Aaron for pointing out the flaw in my spec.

Documentation arity

Before I conclude this post I wanted to mention the optional documetation that can be added to a method / macro / etc by passing the string that documents it in the first position. It reads well and it’s better than standard comments because the documentation is treated as it’s own first class member of the language.

Code

Source for this example can be found on github tagged as added_evaluator.

Getting started with ISpec and Ioke

Saturday, May 16th, 2009

Searching for common ground

Now that I have downloaded and built the source for Ioke E, I wanted to start using it. After all, I made a goal back in January to start using it, but as of yet had failed to follow through with it. Oh sure, I’ve been keeping up with the blog posts and looking at it from time to time, but really hadn’t made any steps to speak of. Until today.

I think that what I really needed was to find some common ground with Ioke. Something I could call familiar and the branch out from. ISpec is where I found it. I found it in two ways really, first in the ability to write specs to test my knowledge of Ioke coding and also in reading existing specs that have been written. These specs run when you download and build Ioke from source, which incidentally, is a painless process I would recommend to any interested.

So what’s next?

Note:I’ve pushed my code out to github tagged as start_ispec

I started out by creating a directory for my project with subdirectories for src and test

~/working $ mkdir ioke-examples
~/working $ cd ioke-examples
~/working/ioke-examples $ mkdir src test

Next I created my first spec as test/bag_spec.ik.

use("ispec")
use("src/bag")
 
describe("Bag",
  it("should have the correct kind",
    Bag kind should == "Bag"
  )
 
  it("should return the values in assigned cells",
    Bag name = "cheeze"
 
    Bag name should == "cheeze"
  )
)

There is nothing really interesting being tested here, the behavior is that of pretty much any Ioke object. The only thing of note is that the use directive is from the root of the project so to specify bag.ik which as a relative path from the spec would be ../src/bag.ik, was actually just src/bag.ik. Next I made an implementation src/bag.ik as

Bag = Origin mimic

This passed. Next I started making a another spec and class. Thinking that I would keep going with this path of thought. I made test/evaluator_spec.ik as:

use("ispec")
use("src/evaluator")
 
describe("Evaluator",
  it("should have the correct kind",
    Evaluator kind should == "Evaluator"
  )
 
  describe("Evaluation",
    it("should evaluate nil as nil"
      Evaluator evaluate nil should == nil
    )
  )
)

and src/evaluator.ik as

Evaluator = Origin mimic
 
Evaluator evaluate = method (
 
)

This results in the following

~/working/ioke-examples$ ispec test
.P..
 
Pending:
 Evaluator Evaluation true (Not Yet Implemented)
 
Finished in 0.26 seconds
 
4 examples, 0 failures, 1 pending

Pretty neat that it sees it as a pending implementation. Overall this was a great success, ISpec was working, but something was still nagging me.

Something isn’t right

Maybe it’s my Ruby roots, maybe it’s that I was being picky but having to specify the source directory when referencing bag.ik and evaluator.ik just didn’t seem right. I needed something like test_helper.rb in Ruby.

I looked around for a while and finally started digging into the test directory for Ioke, which is a great reference for the language. Three cheers for TDD! After a little fiddling I found that I could make a file called test/helper.ik as follows:

use("ispec")
System loadPath << "src"

This automatically loads ISpec and also adds the src directory to System loadPath, which is a list of the locations that Ioke will look for files in the use directive.

This allowed me to change use directives at the beginning of my specs to something like:

use("test/helper")
use("bag")
 
describe("Bag", ....

Comfort level

Now that I have my ISpec basics up and running, I hope to be playing more with this interesting language. Hopefully you will be seeing more Ioke posts here soon.