Ruby 1.9 Hash in Ruby 1.8

Posted by mikong on April 03, 2008

I’m learning about Ruby 1.9 features but I realized I won’t be able to use it in any of my projects. So I thought of an exercise where I learn Ruby metaprogramming by trying to implement some of Ruby 1.9’s new functionality. Aside from learning metaprogramming and Ruby 1.9, I’d also end up with a library that I can use in my Ruby 1.8 projects.

But first, let me introduce…

The new Hash

We have an alternative hash syntax in Ruby 1.9:

  # old way that still works in Ruby 1.9
  my_hash = { :a => 'apple', :b => 'banana' }

  # new way
  my_hash = { a: 'apple', b: 'banana' }

A nice addition in Ruby 1.9 is that the order in which you added the items to a hash is remembered and will be used when the hash is iterated.

There’s also a new class method try_convert where if you call

  Hash.try_convert(myobject)

myobject’s to_hash method will be called to return a hash. If there’s no to_hash, nil will be returned.

And then we have these new instance methods (a few were simply borrowed from the Array class): assoc, compare_by_identity, compare_by_identity?, flatten, key and rassoc.

Trying Metaprogramming

Let’s first try to implement the try_convert class method. Luckily, Chris Wanstrath’s try() article gave us something we could use:

class Object
  ##
  #   @person ? @person.name :nil
  # vs
  #   @person.try(:name)
  def try(method)
    send method if respond_to? method
  end
end

By building on his code above, we could do this:

class Hash
  def self.try_convert(obj)
    obj.try(:to_hash)
  end
end

Looking at that, I’m starting to think Ruby 1.9 should have included the try() method instead of providing try_convert(). But that’s beside the point of this exercise.

Let’s try the simple instance method flatten. The documentation said it converts the hash to an array, then invokes Array#flatten! on the result. So it seems to be simply this:

class Hash
  ...
  def flatten
    to_a.flatten!
  end
end

But looking closer there’s actually a depth parameter (default is 1 - or so it seems but it behaves like -1 in my version of Ruby 1.9) demonstrated in the documentation’s example:

h = { feline: [ "felix", "tom"], :equine: "ed" }
h.flatten    # => [:feline, ["felix", "tom"], :equine, "ed"]
h.flatten(1) # => [:feline, ["felix", "tom"], :equine, "ed"]
h.flatten(2) # => [:feline, "felix", "tom", :equine, "ed"]

It turns out that the flatten and flatten! methods in Ruby 1.9 Array has also changed with a new level parameter. The default value of -1 makes it behave like the original (i.e. it recursively flattens the array). A level value of 0 performs no flattening and a level greater than zero flattens only to that depth (like the depth parameter in the Hash#flatten example above).

The flatten method we implemented above works fine. If you want the depth parameter, I’ve added it by first redefining the flatten method in Array, and then just calling that from the flatten method in Hash. You could head over to my GitHub repository to see the code. The simple project also implements some Ruby 1.9 Time class methods (sunday?, monday?, etc) using the method_missing trick. And I’ll continue to play around with Ruby 1.9 and metaprogramming so you could expect more Ruby 1.9 features in Ruby 1.8. The idea is not to port Ruby 1.9 to Ruby 1.8 (that would be crazy!), but to try stuff so some weird things may also crop up.

Grails and Rails 1

Posted by mikong on February 12, 2008

I didn’t like the idea of checking out Grails. I mean if I’m already a Rails developer, why bother checking a copycat. That’s why I’m surprised I found things I liked about it. But will I leave Ruby on Rails for it? This article is about my first impressions of Grails after reading the book Getting Started with Grails, sections of their online documentation and some Grails-related articles.

Some Good Stuff

After you run ‘grails run-app’ (the equivalent of ’script/server’ in Rails) you can access your application with the URL http://localhost:8080/app_name and get a ‘Welcome to Grails’ page. It’s a minor thing but I like having the application name in there. When you type your application URL by hand in the address bar and you’re shown the cached URLs, it looks more organized. Multiple Grails applications can run together without having to reconfigure URLs. The Welcome page also includes links to the index page of each of your controller so that’s another nice addition. (Note: The URLs to the other pages of a Grails application is accessed by appending controller/action/id to the URL, just like Rails.)

Grails ships with HSQLDB which makes playing with simple applications really easy. You can create your app at the start without thinking about the database. Of course, your data won’t persist this way because HSQLDB stores your data in memory, but I don’t really care about that when I’m just playing around. In Rails 2.0.2, the default DB was changed to SQLite3 so you don’t have to setup your database but you need SQLite3. In Grail’s HSQLDB, you only need Java which you should already have if you’re using Grails.

In Grails, models are ‘domain classes’. Grails domain classes remind me of DataMapper, the Ruby ORM. You list down the properties of the domain in the class, and the framework generates the table with these columns in the database. So instead of adding a column through a migration, just add a property in your domain class. You won’t need an Annotate Models plugin here.

And there are other nice things in Grails. Because Grails is on top of Java, you have i18n support out of the box. The Grails tag library is also a clean approach to views.

Some Bad Stuff

I have a few issues with Grails defaults. The first problem I had with Grails is that the generated DB columns are required or cannot be NULL. You need to declare in your domain class the columns that are optional. I don’t really get this because in most of the applications I’ve worked with, there are more optional columns than required ones. Also, I’m the type of Rails developer that avoids adding constraints in the DB unless I have to. If my application is the only thing that accesses my database, then I’ll put my constraints in one place, the application-side.

One of the many little things I like about Rails is if you have created_at and updated_at columns, they are automatically set when you save a record. You also have the timestamps method added in your generated model migration by default that adds these 2 columns. In Grails, you need to specify a createdAt property in your domain class, and set it manually to new Date().

In contrast to Rails, Grails controllers do not extend an application controller by default. It’s a minor thing but I would probably need to extend it manually for every Grails application I write, hypothetically speaking.

In Grails, you’re not supposed to need to restart your server when you make changes. But this is not entirely true. When you add static constraints to your domain classes, add new messages, or a new Java class, you still need to restart. For Java developers, it’s better than having to build and deploy a WAR file every time, but it’s still a bit irritating to me.

You can mix Java and Groovy in your Grails application. This can be good in a lot of ways but I think it can also be bad. Java developers working on a Grails application may be tempted to use their old, inefficient ways instead of taking the leap to Groovy. Or when Groovy doesn’t support a Java construct a Java developer is used to, you end up with verbose code, like the search implementation in the Getting Started with Grails book (see Working with Java Classes starting at page 63). This was particularly disturbing and made me think: if Java always has Groovy’s back, Groovy might depend on Java too much.

It’s Really the Syntax

My issues with Grails so far are minor but the thing for me is: Ruby/Rails syntax is just more beautiful than Java/Groovy/Grails syntax.

Mapping a has_many association in Rails:

  has_many :registrations

In Grails:

  static hasMany = [registrations:Registration]

Adding a before filter in your controller in Rails:

  before_filter :auth, :except => 'search'

In Grails:

  def beforeInterceptor = [action:this.&auth, except:['search']]

The end of Rails?

I won’t be surprised if Grails becomes more popular than Rails but only because Java is currently the most popular language in the industry (based on TIOBE). Grails is for Java developers who don’t want to leave their comfort zone. It offers something a lot better than the current Java web frameworks and that’s a good thing. But I like Rails better; I might only consider Grails if I have to integrate with Java. And I prefer Ruby over Groovy. Groovy is the dynamic language that looks like Java so that Java developers have an easier transition. That has a cost though, and one may be that it will never be as beautiful as Ruby.

Web Developer v2 3

Posted by mikong on December 31, 2007

As a web developer, it was a year full of changes. My OS for work before was Windows 2000, now it’s Mac OS X. I used Java and a proprietary web framework and now I mainly program with Ruby and Ruby on Rails. I used a proprietary javascript framework, and now Prototype. I primarily used Eclipse as my IDE, now I use TextMate (I used emacs for about a month). My database was Oracle, now it’s MySQL or PostgreSQL. From CVS to SVN, and recently using Git on personal projects. That’s changes to my OS, programming language, web framework, IDE, database and version control system. And here’s more…

From the waterfall software development model to agile software development. And from working with large teams (of 20 to 50 or more) for more than a year on each project to a small team (of 1 to 4) working on 2 week iterations (see Scrum). And as described in this older article, I switched my keyboard layout from QWERTY to Dvorak a few months back.

I didn’t test a year ago. Instead, I passed my code to software testers. I’ve heard of JUnit but never used it. Now, I’m learning BDD with RSpec.

There are also other changes like starting this development blog, participating in the Philippine Ruby Users Group, and contributing to Rails. Using newer releases like Ruby 1.9 (only for playing around though) and Rails 2.0 for my new projects. From Ubuntu Dapper to Gutsy (I skipped Edgy Eft and Feisty Fawn). Et cetera.

2007 was fun! And I’m curious how this article will look like a year from now. :)

Learning Ruby 1.9 6

Posted by mikong on December 25, 2007

I’m not going to list down “improve Ruby skills” on my New Year’s resolution. With the newly released Ruby 1.9.0 (a development release) and beta book Programming Ruby 3 by Dave Thomas (or simply Pickaxe 3), what better time to dig deeper into Ruby than now?

When I setup Rails on my mac a few months back (before Leopard and Rails 2.0), I followed Building Ruby, Rails, Subversion, Mongrel, and MySQL on Mac OS X by Hivelogic. Following the Ruby part of that but updated for Ruby 1.9:

  cd /usr/local
  sudo curl -O  ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.0-0.tar.gz
  sudo tar xzvf ruby-1.9.0-0.tar.gz
  cd ruby-1.9.0-0
  sudo ./configure --prefix=/usr/local/ruby-1.9.0 --enable-pthread --with-readline-dir=/usr/local
  sudo make
  sudo make install

I didn’t have to run ‘make install-doc’ because ‘make install’ already installed the documentation. Since I haven’t upgraded to Leopard yet, this has only been verified on Tiger. Running ‘/usr/local/ruby-1.9.0/bin/ruby -v’, I got

  ruby 1.9.0 (2007-12-25 revision 14709) [i686-darwin8.11.1]

Cool! And I just bought Pickaxe 3 (right after installing). Looking for the first 1.9 feature I can find in the book… a new hash syntax:

  $ /usr/local/ruby-1.9.0/bin/irb
  irb(main):001:0> inst_section = { cello: 'string', clarinet: 'woodwind' }
  => {:cello=>"string", :clarinet=>"woodwind"}
  irb(main):002:0> inst_section[:clarinet]
  => "woodwind"

It works! And let me just add that new hash syntax looks gorgeous. Now I have to end this article and try a 1.9 feature beyond mere syntax updates. Chapter 11: Fibers, Threads, and Processes looks like a good place to start… :)

Update 12/26/2007: I initially installed Ruby 1.9 on /usr/local. It installed rubygems 1.0.1 with it and messed up my gem installation for 1.8.6. I had to reinstall 1.8.6, and then reinstall ruby 1.9 in an isolated directory, /usr/local/ruby-1.9.0. I updated the article to use this directory. For now, I only use 1.9 to try the new features. If you need to work with multiple versions of Ruby, you might want to check out Dan Manges’ article.

My Git notes for Rails

Posted by mikong on December 17, 2007

From Toolman Tim’s blog entry Setting up a new Rails app with Git (with a few changes, and summarized for personal reference).

$ git init
$ mate .gitignore

Add in .gitignore:

log/*.log
tmp/**/*
.DS_Store
doc/api
doc/app
config/database.yml
db/schema.rb

Back to the command line:

$ cp config/database.yml config/database.yml.example
$ touch log/.gitignore
$ touch tmp/.gitignore
$ git add .
$ git commit -m "Initial commit"

Additional Notes:

  find . \( -type d -empty \) -and \( -not -regex ./\.git.* \) -exec touch {}/.gitignore \;

Blogging From TextMate, and RubyConf 2007 videos 2

Posted by mikong on December 13, 2007

This is a test post following an old TextMate screencast demonstrating the blogging bundle.

Continue reading…

PHRUG’s 2nd Rails Coding Session 1

Posted by mikong on December 07, 2007

Last Wednesday, I attended the 2nd Rails Coding Session of the Philippine Ruby Users Group (PHRUG). The small, informal gathering was planned for writing Rails apps with plugins like acts_as_solr, memcached, etc. None of us wrote a single line of code. Instead, we discussed several Rails and Ruby topics and gathered ideas for future Rails events.

Some of the topics discussed were:

The future Rails events planned are coding sessions (hopefully with actual coding involved), monthly meetups (with the next meetup probably in January and to be held at either the Ateneo de Manila University campus or Stratpoint Technologies office), and possibly a Philippine Rails Conference. Some notable issues covered were the challenges of organizing a large conference (including problems involved with having closed source companies as sponsors), and  problems with matching the level of explanation in your presentations with the knowledge of your audience.

The Rails community in the Philippines is only starting to grow. But I’m glad that the local Rails events are increasing in both frequency and number of participants. Previous Rails events I’ve attended are the First PHRUG Meetup last January, Rails Happy Hour last September, Rails Talk with the presentation by Guy Naor last October, and most recently this “coding session” (I missed the 1st Rails Coding Session 2 weeks ago).

Before I end this post, I’d like to thank Rad and the Development Executive Group for accommodating us for the session.

A Journey with Dvorak so far 3

Posted by mikong on November 27, 2007

About 2 months ago, I switched from QWERTY to using the Dvorak keyboard layout. As an aside, the layout is called Dvorak because it was patented by Dr. August Dvorak - the upper left keys are NOT replaced with D, V, O, R, A, and K as some people have clarified with me (see Wikipedia article). The Dvorak layout was suggested to me by a friend who switched months earlier and I agreed after some serious deliberation. But this article is not about the deliberation, rather how well it is going so far…

Last weekend, I reached over 60 wpm in a typing test. This is still below my QWERTY stats of an average speed of 65 wpm and a burst speed of 95 wpm. But it is going surprisingly well and generally pleasant.

To learn Dvorak, the process I followed was quite simple. For 2 weeks, I trained for about an hour a night by working on the exercises in A Basic Course on Dvorak. I just ran each lesson there twice. Outside the training, I still used QWERTY. After finishing the course, I made the full switch and avoided QWERTY almost entirely. I no longer used training tools, except every weekend to track my improvement.

My typing speed improved steadily until about 50 wpm. At the end of the 2-week training, it was a little over 20 wpm. I reached 30 after a week, 40 after another, and then 48 wpm. It slowed down significantly since then. With 60 wpm today, I improved only by 12 wpm after 4 weeks. But I’ve reached my targets so far by improving by 10 wpm until 40 wpm, the minimum required typing speed for clerk-typists. I target to reach my old QWERTY average of 65 wpm by the end of the year.

It was only difficult in the beginning, and only frustrating when participating in chat conferences. For some strange reason, I had more chat conferences than usual (more than the 1st 9 months of the year). I also noticed that typing punctuations needed to be exercised to get used to them. Fortunately, punctuations are used regularly in programming. Also, it’s useful to have the Dvorak keyboard layout wallpaper to cheat. :)

ActionController RSpec: stub Time.now

Posted by mikong on November 23, 2007

So I’m playing with the mocks and stubs in RSpec and encountered a failing example. I’m setting the Task.completed_at column to Time.now in my controller like this:

  def complete
    @task = Task.find(params[:id])
    @task.update_attribute :completed_at, Time.now
  end

and this line in my RSpec example fails:

  @task.should_receive(:update_attribute).with(:completed_at, Time.now)

It’s fairly obvious that Time.now is the problem. Don’t let the error message fool you!

...
Mock 'Task_1002' expected :update_attribute with (:completed_at, Sat Nov 24 00:43:02 +0800 2007) but received it with (:completed_at, Sat Nov 24 00:43:02 +0800 2007)
...

The values being compared look the same in the error message, but as stated in the Ruby documentation, “The [time] object will be created using the resolution available on your system clock, and so may include fractional seconds.” I created a stub and my RSpec example now looks like this:

  it "should tell the Task model to update the task's completed_at to the current time on POST to complete" do
    # Stub
    time_now = Time.now
    Time.stub!(:now).and_return(time_now)

    Task.should_receive(:find).with("1").and_return(@task)
    @task.should_receive(:update_attribute).with(:completed_at, Time.now)
    post 'complete', {:id => "1"}
  end

I could have made time_now an instance variable, and moved it to before(:each) so that it can be reused by other RSpec examples. But at the moment, this is the only example that needs it.

Alternatives?

I Googled to check how the others implemented this. One of the results showed a year-old mailing list discussion of stubbing Time.now this way:

    @time_now = Time.parse("Jan 1 2001")
    Time.stubs(:now).returns(lambda{@time_now})

The discussion said that’s supposed to work before but there’s an error now regarding the number of arguments, and it was mentioned it could be a bug. Another suggested defining Time.now in spec_helper. Other results weren’t in the context of RSpec.

I’m satisfied with my code for now. I’ll make a helper in spec_helper if other specifications will need it.

Working with Rails is no longer just a database of Rails developers

Posted by mikong on November 22, 2007

First off, I’d like to congratulate Working with Rails on their 1st birthday! With the size of their community, I can’t believe they are only a year old. If you’re using Ruby on Rails and haven’t joined yet, you should go check it out.

What some people don’t know is that Working with Rails is not just a site of Rails developer profiles. Well, it is primarily that, and the site helps hiring companies and job-hunting Rails developers find each other. This has been made more explicit by their new Job Board. But more than that, they have a few other things to offer.

One thing I find really helpful is the way you can browse the top Ruby gems and Rails plugins. The list is ordered by usage of the entire community or weighted by authority. You could even take a look at the gems and plugins used by a particular developer (here are my gems and plugins). It’s a shame that currently this info is not available for the very top Rails developers (because a gem is needed to install and run to gather the data). Read more about this in “What’s in your toolbox?”.

Then there’s the Hackfests, or monthly contests where the top contributors to the Ruby on Rails source code are rewarded. If you’re a Rails developer from a third world country and find that O’Reilly books are a bit over your budget, you might want to join the contest and get about 4 patches applied to the source code within a month.

There’s also the Rails BlogSphere where you can be updated with the latest blog posts by your favourite Rails developers. I haven’t really used this much because I think I have subscribed to enough Rails blogs already.

The site also has a forum for discussing anything about Rails, but I don’t really recommend it, or at least not yet. The forum activity is quite low. If you need help with your Rails project, I think you may be better off asking in the official mailing lists of Rails or of your local Rails community.

Working with Rails has grown a lot in just a year in both the size of the community and site features. I hope they grow even more in their 2nd year.