Archive for category ruby

Ruby Basics – Equality operators in Ruby

Dig deeper into Ruby with this book

Dig deeper into Ruby with this book

After Greg Sterndale’s presentation on a boston-rb hackfest earlier this month I noticed that not everyone knew the operators available for equality and comparisons in Ruby. Why not take the dust away from the blog and write about it, then?

Ruby has many equality operators, some of them we use and see everywhere in our applications (like the usual double equal – “==”) and some are also used everywhere but we don’t really get to see them (like the triple equal or case equal operator – “===”). So let’s dig into how Ruby implements comparisons between our objects.

You can see the full source code for this tutorial on Github.

Continue reading “Ruby Basics – Equality operators in Ruby” »

Tags: , , , , , , , ,

Handling various rubies at the same time in your machine with RVM – Ruby Version Manager

Dig deeper into Ruby with this book

Dig deeper into Ruby with this book

If you’ve been working in Ruby for more than a year you have probably seen a lot of changes in the landscape. We saw a lot of gems adding compatibility layers to run on Ruby 1.9.2, Rails 3 was finally released (also supporting 1.9.2) and new gems using 1.9.2 features are also showing up.

But guess what? You’re stuck at 1.8 (1.8.7 if you’re lucky) for a lot of projects and you’re in deep fear that installing the latest Ruby 1.9.2 to try all these new fancy things is going to wreak havoc on your environment and you’re going to be FUBAR.

Worse, you still have projects running on Ruby 1.8.6 (with that nasty SMTP-TLS bug) and if you upgrade to a newer Ruby you might have false positives in your codebase and things are going to break in production. You’re already FUBAR, you might think.

But fear not! There’s a knight in shiny armor riding for his damsel in distress! (yes, YOU) And this knight is RVM!

Continue reading “Handling various rubies at the same time in your machine with RVM – Ruby Version Manager” »

Tags: , , ,

Full text search in in Rails with Sunspot and Solr

The book you should get to dig deeper into Solr

The book you should get to dig deeper into Solr

Click here if you want to see a PDF version of this tutorial.

Full source code for this tutorial is available at GitHub.

Everyone wants to take their databases to run everything as fast as possible. We usually say query less, add more caching mechanisms, add indexes to the columns being searched, but another solution is not to use the database at all and look for better solutions for your querying needs.

Continue reading “Full text search in in Rails with Sunspot and Solr” »

Tags: , , , , ,

Deployment Recipes – Deploying, monitoring and securing your Rails application to a clean Ubuntu 10.04 install using Nginx and Unicorn

Agile Web Development With Rails

Agile Web Development With Rails

You can get a PDF version of this tutorial here.

If you’re a developer, setting up a server might not look like something you’d do yourself. Most of the time you have a sysadmin in your team/company/business that can do the job for you. But knowing how to configure a server might make your life easier if you’re rolling on your own so let’s take a look at how we can configure a clean slate Ubuntu 10.04 server machine to run a Ruby on Rails application.

This example used a just created Ubuntu 10.04 image from Rackspace, but you should be able to follow it on any Ubuntu 10.04 install, even a local one.

With this tutorial you’ll learn how to:

  • Install the libraries usually necessary to run Ruby on Rails application;
  • Setup Nginx as the HTTP server proxy and statics assets server;
  • Setup Unicorn as the application server that’s going to run your application;
  • Setup Monit to watch over the processes
  • Setup some basic firewall rules
  • And finally deploy your Ruby on Rails application to it;

Remember that this tutorial is given in an “as is” basis and you should backup all system files we’re changing here.

Continue reading “Deployment Recipes – Deploying, monitoring and securing your Rails application to a clean Ubuntu 10.04 install using Nginx and Unicorn” »

Tags: , , , , ,

Asynchronous email deliveries using Resque and resque_action_mailer_backend

The Rails 3 Way

The Rails 3 Way

Check the GitHub repo here and a sample application using it here.

If you have ever sent emails using ActionMailer during a user request you probably noticed that if the email sending fails or takes too long your user might not be really happy with the speed of your application. Making the email sending process an asynchronous one is usually the simplest solution for this problem and there are plenty of tools to do that like ar_mailer, that stores emails in your database and then uses a specific daemon to send them.

Continue reading “Asynchronous email deliveries using Resque and resque_action_mailer_backend” »

Tags: , , ,

If you’re cleaning up your user’s input in your views you’re doing it wrong

Have you ever found yourself using the “h” view helper all around your views in your applications? Have you ever thought that cleaning up user input in views is a tedious, error prone and cumbersome job?

You’re not alone.

Continue reading “If you’re cleaning up your user’s input in your views you’re doing it wrong” »

Tags: , , , ,

Building your own ActiveRecord validation macros with validates_each

A common task when writing your own Rails applications using ActiveRecord is creating your own validations for your models. While it’s perfectly correct to add the validation directly into the model you’re going to need it, sometimes you’d like to reuse the same validation logic in other models and we’re not really going to do a cut-and-paste here are we?

Continue reading “Building your own ActiveRecord validation macros with validates_each” »

Tags: , , ,

Setting a far future expires header for your Rails app static assets in your Nginx server

Even Faster Websites - Everything you need to now to improve you website's front end performance

Even Faster Websites

Setting a far future expires header for your static assets is one of the first front end performance improvements you must do in your web applications. This will tell your user’s browser to keep the static assets cached and they won’t make unnecessary HTTP requests just to see that the version they currently have is already the latest and this will surely improve your website’s perceived performance (yes, there’s a REAL and a user perceived performance for every application).

Continue reading “Setting a far future expires header for your Rails app static assets in your Nginx server” »

Tags: , ,

Accessing the current request object on your mailer templates to generate links

A common issue with mailer templates is that as they’re not being called from a controller you can’t get your hands on the request object and access properties like host_with_port. While you’re usually calling the mailers inside controllers and you could possibly hand the request as a parameter to it, it isn’t really nice to do this every time you need to send an email.

<%= link_to 'Home', "#{current_request.protocol}#{current_request.host_with_port}/home" %>

So, if you’re looking for a quick and easy solution to this issue, the current_request plugin is your friend, you can install it by calling:

ruby script/plugin install git://github.com/mauricio/current_request.git

The plugin works by setting the current request in a thread local variable that will be available until the end of the request, which means that you can use it safely in your templates, two new methods are added to all views, current_request, that returns, obviously, the current request being answered and current_host that will build the current host with port and protocol for you. Examples:

Or you can just use a shorthand to the current host:

< %= link_to 'Home', "#{current_host}/home" %>

You can also use it wherever you want to access the current request (and not only on templates) by calling:

CurrentRequest::Holder.current_request

Why I am not using Masochism for my master-slave setups and why monkey-patching isn’t the only solution

I got a message this morning from Gregg at Ruby5 asking why I wrote the master_slave_adapter plugin instead of using Technoweenie’s Masochism and I think the answer to this question deserves a little blog post (and the blog really needs some new content :P).

Continue reading “Why I am not using Masochism for my master-slave setups and why monkey-patching isn’t the only solution” »