Bmore on Rails – Sept. 10 2008

10 09 2008

Last night, I attended Baltimore on Rails for the first time and I was very pleased with the presentations and conversation.

The first talk was from Bryan Liles. He actually had two distinct topics. His first one was a just a quick survey of Ruby REST clients. He quickly went through a list of them, while pointing out some of their pros and cons:

Bryan’s second talk was entitled “10 things you should do to your code”. You can find the content of his presentation on his blog.

After Bryan’s talk, Frank Tao presented HyperActiveResource, a set of enhancements to ActiveResource that his team wrote. They have added a bunch of features to ActiveResource without monkey-patching it. For example, you can use :include like you would with ActiveRecord. To me, it just feels like a lot of the features they added should already be in ActiveResource. I hope that they can get some of these enhancements in there.





RailsConf Europe 2008 – Day 1

7 09 2008

Ahson and I arrived in Germany on Monday to attend RailsConf Europe thanks to FiveRuns. On Monday night, we attended Bratwurst on Rails. It was a great event and the bratwursts were delicious.

Tuesday was the first day of the conference and I attended two tutorials and a panel with the Rails Core members. The first tutorial was given by Pradeep and Michael from Intridea, a rails consulting firm based in Washington DC. The title of the tutorial was The Renegade’s Guide to Hacking Rails Internals. The second tutorial I attended was given by two ThoughtWorkers: Neal Ford and Patrick Farley. The topics were MetaProgramming and Ruby internals. The summary of the two tutorials and the panel is below.

The Renegade’s Guide to Hacking Rails Internals

Pradeep started the tutorial by listing the reasons why you would want to hack Rails internals:

  • you’ll learn a lot
  • know your tools
  • you may have to
  • you can do very cool stuff: asynchronous goodness
  • keeps your app DRY
  • keeps multiple apps DRY
  • effective ownage: own your codebase

With that said, he made sure to emphasize that hacking the internals of Rails can be dangerous, so be careful and only do it if you really need to. And when you do it, cover your ass with tests.

The first piece of Rails code that we started to look at was the Rails initialization process. A good point of entry to start understanding what is going on there is script/server. Pradeep walked us through the steps involved in the initialization of rails. In particular, we looked at boot.rb, initializer.rb, and environment.rb. To get a good idea of all Rails does during initialization, you can look at the ‘process’ function in initializer.rb.

The next thing we looked at was the Rails class loader defined in ActiveSupport::Dependencies. One thing to notice there is the way rails load classes in a production environment vs. a development environment. In dev mode, rails uses ‘load’, while in prod, it uses ‘require’. This logic can be found on the function ‘require_or_load’ in dependencies.rb (of ActiveSupport).

Below is a quick example of class loading in Rails:

has_many :users
:users => User (not loaded yet)
ActiveSupport::Dependencies.load_missing_constant(:User)
(searches through Dependencies.load_paths)
(finds the first file that matches => app/models/user.rb)
require_or_load(‘app/models/user.rb’)

After that, Pradeep did a lot of live coding. He showed us how he wrote a Rails plugin that hacks the Rails internals. The plugin he wrote is similar to app-slices in Merb. It lets you dump a whole Rails-like directory structure of code into the app/slices directory and it picks everything up from there as if it was part of your Rails root folder. However, if you create a controller in app/controllers with the same name as a controller in your slice (e.g. app/slices/blog/app/controllers), the actions from the app/controller will override the actions from the slice controller.

There are other plugins for rails that do similar things:

Pradeep live-coded the whole plugin in front of the audience. It was a great presentation and a great way to learn about the Rails internals. He promised to release the code and the slides so watch the Intridea blog if you are interested.

Michael Bleigh took over for the second part of the tutorial. He showed how he wrote the subdomain_fu plugin, which ‘provides a modern implementation of subdomain handling in Rails’.

subdomain_fu doesn’t require as much hacking of the internals but it was a good example to show how you sometimes have to dig into the internals to understand what you are doing when writing a plugin. The plugin is small so I would recommend that you take a look at the code on github and try to understand it. It consits of a few helper methods in subdomain-fu.rb and a simple alias_method_chain in url_rewriter.rb. The trickier part to write was the route extension, but with the help of Jamis Buck he was able to get it working.

Meta-programming Ruby for Fun & Profit

Neal’s meta-programming talk was interesting and his slides had some interesting code samples. You can download them from here. Some of the slides I had already seen at RubyNation during his Ceremony vs Essence talk.

Here are a few things that I noted from Neal’s talk:

  • Jim Weirich’s BlankSlate just metaprogramically removes all methods from an object. It was adpopted in Ruby 1.9 where Object extends from BasicObject (which is basically BlankSlate).
  • sapir-whorf hypothesis: language affects the capabilities of thoughts
  • Even though Ruby is more powerful than Java, it has a smaller surface area. Matz created a super powerful language that is very simple at its core.
  • Features from weaker languages can be synthesized in more powerful languages.
  • Ruby is all about messages sent to objects.
  • dust gem: makes it easier to write easier/safer test

Patrick Farley’s portion of the tutorial was more about Ruby Internals. A lot of the talk was similar to his talk at Mountain West Ruby Conf. I highly recommend watching this video as you’ll learn a lot from it. In particular, the following will start making sense if it doesn’t already:

struct RObject {
struct RBasic basic;
struct st_table *iv_tbl; # stands for Instance Variable table. a C Hash.
}

struct RBasic {
unsigned long flags; # is this object tinted, frozen, is it a singleton class… just bit flippin
VALUE klass; # in this case VALUE is pointer to a location in memory where a class lives
}

struct RClass {
struct RBasic basic;
struct st_table *iv_table;
struct st_table *m_tbl; # Method table where all the behavior is stored
VALUE super; # stores super class
}

During the discussion of the Ruby Object model, we discovered one of the differences between the dup and clone function, as highlighted by this code. Once you watch the video, it will all make sense. Otherwise, buy these screencasts and watch them. It’s important to understand Ruby’s object model.

Panel Discussion with DHH and Rails Core Members

This was a Q&A with 3 members of the Rails core team: DHH, Jeremy Kember, and Michal Koziarsky. They started by announcing that there was a new committer and that 2.0.4 will be released (which they did the next day).

There were a lot of questions and I didn’t take extensive notes but there are two things that I remember particularly:

  • They said how git has been wonderful in allowing them to cherry-pick good patches and ideas. They were skeptical before the move of how it was going to affect their development cycles but it seems like it was for the best.
  • When asked about the competition in Ruby web frameworks, DHH said: ‘Variety is great when there is variance’. But he did acknowledge that when they see good things in these other frameworks, they can apply them to Rails (Rack comes to mind)
  • That’s all I have. Stay tuned for my coverage of Day 2 coming soon








    Follow

    Get every new post delivered to your Inbox.