Logo

Is Ruby interpreted or compiled?

8 days ago | Pat Shaughnessy: Pat Shaughnessy - Home

Both JRuby and Rubinius can compile your
Ruby code into machine language!

Ever since I started to work with Ruby in 2008, I’ve always assumed that it was an interpreted language like PHP or Javascript – in other words, that Ruby read in, parsed and executed my code all at runtime, at the moment my program was run. This seemed especially obvious since the default and most popular implementation of Ruby is called “MRI,” short for “Matz’s Ruby Interpreter.” I always thought it was necessary to use an interpreter to make all of the dynamic features of the language possible…

Journey to the center of JRuby

16 days ago | Pat Shaughnessy: Pat Shaughnessy - Home

Could Jules Verne have imagined something
as complex as the Java Virtual Machine?

This week I decided to take a look at JRuby, which is a Java implementation of the Ruby interpreter. I’ve heard great things about JRuby, but had never taken the time before to look at how it works internally. What I found was an amazingly complex system involving many different technologies. Following the path of execution through the JRuby stack was like peeling away different layers of an onion – there was always another layer inside.

Read on to join me on a journey through all of these layers; we’ll see how a simple Ruby String method call is handled by Java, how it’s translated into Java byte code, and, ultimately, how that is translated into native machine language by the JVM’s Just In Time (“JIT”) compiler. While you may not be convinced JRuby is the right platform for you – certainly just the hearing word “Java” is enough to send chills down my spine – I hope you’ll come away with a new appreciation of the elegance and sophistication of what the JRuby team has built…

A chat with Nick Quaranto about RubyGems.org internals

22 days ago | Pat Shaughnessy: Pat Shaughnessy - Home

RubyGems.org has made it much easier
for all of us to contribute Ruby gems

Nick Quaranto (@qrush) revolutionized gem authoring in 2009 by launching a new gem repository called Gemcutter.org. Suddently for the first time any Ruby developer could publish a new gem simply by running “gem push my_awesome_gem.” The speed and simplicity of this new process caused an explosion of Ruby gem development and publishing. Gemcutter.org was later moved to RubyGems.org and became the Ruby community’s default gem repository.

I enjoyed listening to Nick chat with the RubyRogues about RubyGems.org a couple of weeks ago, especially the stories about how Nick got started developing Gemcutter and it’s early history. Then last week I had the opportunity to chat with Nick about how RubyGems.org actually works. I was curious to know more about what happens on the server when I push a new gem file, how it serves gems to everyone so quickly, and how it works with the new Bundler 1.1 dependency API. You can read the highlights of our conversation over on RubySource.com

My first impression of Rubinius internals

29 days ago | Pat Shaughnessy: Pat Shaughnessy - Home

Reading the Rubinius source code is like
walking through a well-kept, manicured garden

This week I decided to take a look at Rubinius; I had heard for a long time that somehow the Rubinius team had “implemented Ruby with Ruby.” That sounded both intriguing and impossible at the same time; I just had to take a look. But even the name itself intimidated me: hearing “Rubinius” somehow led me to expect to see something esoteric or arcane, possibly because it rhymes with “devious.” But since I had just spent a few weeks studying the MRI Ruby interpreter’s C source code, I felt I was ready for anything.

I ended up being pleasantly surprised! Instead of something arcane or cryptic, what I found was beautiful, well documented and easy to understand code…

Seeing double: how Ruby shares string values

about 1 month ago | Pat Shaughnessy: Pat Shaughnessy - Home

How many Ruby string values can you see?

How many times do you think Ruby allocates memory for the “Lorem ipsum…” string while running this code snippet?

str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit"
str2 = str

…or what about while running this snippet?

str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit"
str2 = String.new(str)

…and this one?

How to Customize Twitter Bootstrap’s Design in a Rails app

about 1 month ago | Pat Shaughnessy: Pat Shaughnessy - Home

You can find part 3 of my Twitter Bootstrap and Rails series over on rubysource.com. I plan to post again here on my site next week with more on MRI Ruby internals, following up on some of the issues I discussed last week with Ruby strings.

Never create Ruby strings longer than 23 characters

about 1 month ago | Pat Shaughnessy: Pat Shaughnessy - Home

Looking at things through a microscope
sometimes leads to surprising discoveries

Obviously this is an utterly preposterous statement: it’s hard to think of a more ridiculous and esoteric coding requirement. I can just imagine all sorts of amusing conversations with designers and business sponsors: “No… the size of this <input> field should be 23… 24 is just too long!” Or: “We need to explain to users that their subject lines should be less than 23 letters…” Or: “Twitter got it all wrong… the 140 limit should have been 23!”

Why in the world would I even imagine saying this? As silly as this requirement might be, there is actually a grain of truth behind it: creating shorter Ruby strings is actually much faster than creating longer ones…

Learning from the masters part 2: three metaprogramming best practices

2 months ago | Pat Shaughnessy: Pat Shaughnessy - Home

Mozart’s Requiem Mass in D minor would be
hard to understand without a translation from Latin

Metaprogramming has always been one of the most intimidating features of the Ruby language – it’s considered very “advanced” and can often be very difficult to read and understand. However, I agree completely with what Russ Olsen said about metaprogramming on this Thursday’s Ruby Rogues podcast: that Ruby metaprogramming really isn’t all that hard to understand, that it is just another moderately difficult programming concept any Ruby developer can learn and use.

Like I did two weeks ago I decided to take a look at the Rails source code, this time to see whether there were any good examples of metaprogramming that I could learn from…

Too good to be true! Twitter Bootstrap meets Formtastic and Tabulous

2 months ago | Pat Shaughnessy: Pat Shaughnessy - Home

Today I posted Too good to be true! Twitter Bootstrap meets Formtastic and Tabulous on rubysource.com, a follow up to Twitter Bootstrap, Less, and Sass: Understanding Your Options for Rails 3.1 that I wrote back in November.

Learning from the masters: some of my favorite Rails commits

3 months ago | Pat Shaughnessy: Pat Shaughnessy - Home

Reading the Rails source code is like looking
at the score of a Bach sonata or partita

In the last month or two, there have been many great commits to Rails. Last week alone we saw: 0306f82 – implements automatic EXPLAIN logging for slow queries and a382d60 – ActiveRecord::Relation#pluck method. There were also many other useful commits that didn’t get so much attention, such as 562583c, 85b64f9, etc., etc. Hearing about all this great work inspired me to take a closer look at the Rails github repo history, to see which commits were the most interesting or creative – commits that would teach me something and help me improve as a Rails developer.

I ended up being surprised! There are plenty of examples of elegant code, cool testing techniques and great new features in Rails if you go and look for them, but what impressed me the most were the small things…

Two ways of using Redis to build a NoSQL autocomplete search index

3 months ago | Pat Shaughnessy: Pat Shaughnessy - Home

Consider a NoSQL solution such as Redis
the next time you need to implement search...

Last week I demonstrated how to setup autocomplete in a new Rails 3.1 app using the Soulmate gem, from SeatGeek. Soulmate uses Redis to cache all of the autocomplete phrases in memory, providing lightning fast query results. While autocomplete is a very useful feature and a common web site design element, what really interests me about Soulmate are the ideas and detailed techniques behind how it uses a NoSQL Redis database to implement autocomplete search…

Finding your soulmate: autocomplete with Redis in Rails 3.1

3 months ago | Pat Shaughnessy: Pat Shaughnessy - Home

Fast autocomplete with Soulmate can help
users fall in love with your Rails app

Back in February the SeatGeek team open sourced a gem they call Soulmate that implements autocomplete using a Redis back end. “Soulmate finishes your sentences” as they say on their Github readme page. You can see it in action on SeatGeek.com. Soulmate is very useful: many of us need type ahead search behavior and using Redis is a great way to make it fast and snappy. But more importantly, Soulmate is a great example of how to create an index in Redis ahead of time, allowing for very fast lookups later. Take a few minutes to learn how Soulmate works; chances are you’ll be able to use the same approach in your own app with a completely different data set…

Twitter Bootstrap, Less, and Sass: Understanding Your Options for Rails 3.1

3 months ago | Pat Shaughnessy: Pat Shaughnessy - Home

You can find my article this week over on rubysource.com.

Besides being faster, what else is new in Bundler 1.1?

4 months ago | Pat Shaughnessy: Pat Shaughnessy - Home

Bundler 1.1 can help you sort through your gems

Three weeks ago I explained why Bundler 1.1 will be much faster than Bundler 1.0, which is clearly its most important new feature. However, there are a few new commands and options the Bundler team implemented in version 1.1 that can help you better manage and sort through the gems you have installed on your development computer and servers. Bundler 1.1’s new commands will tell you which gems you can upgrade, and also help keep things clean by deleting gems you no longer need…

Don’t be terrified of building native extensions!

4 months ago | Pat Shaughnessy: Pat Shaughnessy - Home

It's time for all Ruby developers to
confront their worst fear...

One of the scariest moments in the life of a Ruby developer is seeing that dreaded message appear as you try to install a new gem: Building native extensions. This could take a while…. As the console output suddenly hangs, you feel a horrible, sinking sensation of fear in your stomach: something truly bad is about to happen! Then your worse nightmare comes true: in the console you get: ERROR: Failed to build gem native extension. Now you’re condemned to spending hours googling for obscure compiler flags, environment settings or Linux package info – and not doing Ruby development.

For years I’ve been nervous installing gems that contained native extensions because I wasn’t familiar with the gem extension build process, and had no idea where to begin when something went wrong. Today I’m going to confront my own worst fears – and yours – by taking a closer look at what “Building native extensions” actually means…

Show some love for prepared statements in Rails 3.1

4 months ago | Pat Shaughnessy: Pat Shaughnessy - Home

@Tenderlove and the rest of the Rails core team
deserve some love for speeding up your app!

We’ve heard a lot about many of the great new features in Rails 3.1: the asset pipeline, Coffee Script, HTTP streaming and on and on. But if you’re still using ActiveRecord with a traditional SQL database, like me, then you’re probably using one of Rails 3.1’s most powerful new features without even realizing it: Prepared Statements.

Database servers such as Postgres and Oracle for years have allowed client applications to preprocess and cache specific SQL statement patterns ahead of time, later allowing the query results to be returned even faster…

Why Bundler 1.1 will be much faster

4 months ago | Pat Shaughnessy: Pat Shaughnessy - Home

Bundler is about to get a lot faster...

If you’ve worked on a Rails 3 application during the past year or so you’ve probably noticed that running bundle install or bundle update can often take a long, long time to finish. The terminal seems to hang for 30 seconds or more after displaying “Fetching source index for http://rubygems.org/.” Well I have some good news for you: the smart people behind Bundler and RubyGems.org have come up with a solution, and the new version of Bundler about to be released is a lot faster! Today I’m going to take a look at exactly why Bundler 1.1 is so much faster – at what exactly the RubyGems/Bundler teams did to speed it up…

It’s time to clean up your mess: refactoring Cucumber step definitions

5 months ago | Pat Shaughnessy: Pat Shaughnessy - Home

My step_definitions folder looks like this...

This week I decided to look through my features/step_definitions folder after reading Aslak Hellesøy’s post from Wednesday about removing web_steps.rb. I was worried that I might need to write many more custom steps since web_steps.rb will disappear the next time I upgrade Cucumber.

What I found was a mess! First of all, there were a lot of step files and no naming convention that might help me find a particular step, and each step file was a loose collection of step definitions that weren’t organized or DRY…

The technology you never knew you were using to test your Rails site

5 months ago | Pat Shaughnessy: Pat Shaughnessy - Home

Libxml2 powers your BDD/integration tests
that use the default Capybara driver

If you’re a Rails developer using Behavior Driven Development, then you’re probably using either Cucumber features or RSpec integration tests with the Capybara gem to simulate what a user would do with a real browser. You probably know that by default Capybara uses a driver based on Rack to connect to your Rails app and make simulated HTTP requests, all within the same process using only Ruby…

How does Bundler bundle?

5 months ago | Pat Shaughnessy: Pat Shaughnessy - Home

Every time you start a new Ruby project, probably one of the first things you do is write a Gemfile and then run bundle install. Bundler has truly made all of our lives much easier… we know it will install the proper version of all the gems we need. But how does it actually work? This week I decided to take a closer look at the Bundler code itself to find out. How does it know which gems to install? How does it guarantee each gem’s dependencies will be satisfied? How do I get just the right “bundle” my app needs?

Bundler’s Best Kept Secret

5 months ago | Pat Shaughnessy: Pat Shaughnessy - Home

Part of a gem dependency network graph

This week I just discovered Bundler’s best kept secret: the bundle viz command will generate a network graph showing the dependencies among all the different gems used by your Ruby app. For example, the image on the left is a portion of the gem dependency graph for a vanilla Rails 3.1 app. Click here to see the entire, uncropped dependency graph. The gems actually called out in your Gemfile are displayed in grey, while other gems included through dependencies only are shown in white. Finally gem groups are shown as rectangles along the top…

How does Kaminari paginate?

6 months ago | Pat Shaughnessy: Pat Shaughnessy - Home

“Kaminari” means thunder in Japanese

Kaminari is a popular new gem that provides pagination behavior – to learn how to use it see the Railscast Pagination with Kaminari, or just refer to the Github readme page. For me Kaminari is a good example of “Rails magic…” somehow by just adding the gem to my Rails application all of my models get the Kaminari page method. I don’t have even have to type a single line of code in my model… it’s just automatically added for me. Then when I call page, it immediately works: returning just the records for a given page, even working in conjunction with other scopes I might have…

Where does my javascript code go? Backbone, JST and the Rails 3.1 asset pipeline

8 months ago | Pat Shaughnessy: Pat Shaughnessy - Home

One of the best things about the Rails framework is that each project looks the same:

Rails server code

Every Rails developer knows to look in “app/models” for the model classes, “app/controllers” for the controller classes, etc. When you start a new project all of this is setup for you, and if you’re a new developer assigned to an existing Rails project you know where to look for things. The way you save and organizing your code files is not simply a convenience… I think it actually effects the way you think about code. Well organized code directories leads to well organized code!

Contributing a new scaffold to ScaffoldHub

10 months ago | Pat Shaughnessy: Pat Shaughnessy - Home

Last month I launched http://scaffoldhub.org, a gallery of different Rails scaffolding generators. Initially I posted three scaffolds myself to get the site started.

I just updated the site now to allow anyone to login and contribute new scaffolds… if you like the idea please think about contributing a scaffold! I hope it can someday become a helpful resource for the Rails community. If you do decide to try to write and contribute a scaffold: thanks! Please contact me anytime and I’ll try my best to answer any questions, improve the process or fix bugs…

View Mapper for Rails 3: ScaffoldHub

12 months ago | Pat Shaughnessy: Pat Shaughnessy - Home

Back in 2009 I wrote a gem called View Mapper which created different variations on the standard Rails scaffolding user interface. This was cool because it could show you how to use a certain plugin or gem, and it even worked with existing models and their associations.

This year I’ve been thinking about how to upgrade View Mapper to work with Rails 3 properly… and in the end I decided not to upgrade View Mapper at all but instead to create something entirely new: ScaffoldHub

Customizing Toto to support blog post categories

about 1 year ago | Pat Shaughnessy: Pat Shaughnessy - Home

On this blog I categorize my posts using a few tags, such as http://patshaughnessy.net/tags/paperclip or http://patshaughnessy.net/tags/view-mapper for example. Today I’m going to walk through how I customized the Toto blog engine to display these category pages.

Of course I could have gotten categories for free using a different blog engine such as Jekyll or Nesta, but to be honest when I saw Toto’s code for the first time I fell in love with it: concise, elegant and simple. I just couldn’t wait to try to understand how it works and modify it to do something new. I suppose it really is “the 10 second blog-engine for hackers!”

4 tips for how to customize a Toto blog site

about 1 year ago | Pat Shaughnessy: Pat Shaughnessy - Home

During the week between Christmas and New Year’s I finally took the time to redesign and redeploy this site on Heroku using Toto, a very simple Rack blog engine. Sorry if I spammed your RSS inbox with duplicate articles.

What makes Toto fantastic is that it includes just enough code to run a very simple blog site… and nothing else. Alexis Sellier’s code is concise and elegant; you can see it all in a single Ruby file: toto.rb. It leaves you a clean, blank slate to add anything special or custom you might need. In fact, I chose to use Toto instead of Jekyll, Nesta or other blog engine alternatives only because it looked like it would be fun to understand how it works and customize it…

CodeBuddy: See your Ruby stack come alive!

about 1 year ago | Pat Shaughnessy: Pat Shaughnessy - Home

Alex Rothenberg, Daniel Higginbotham and I just published a new gem called CodeBuddy that displays a Ruby stack trace inside your browser like this:

The idea is that you can see both the stack trace and code snippets from the corresponding files at the same time, to make it easier to understand what was going on. You can move up and down the stack just by pressing the up/down arrow keys, or by clicking on a line in the stack trace: