Category Archives: Development

Anything and everything related to development of Rails applications–from “hello world” and how-tos, tutorials, questions, etc. to higher-level discussion of the best implementation of features, problem-solving, etc.

Conditional Code

You’re writing some code for a new client. Debug statements litter the code. Oops, you sent that to the client. Oops, you’re printing out credit-card numbers. Identity theft ensues. Don’t move to Mexico–you can actually handle this problem in an elegant way by using conditional code statements INSIDE your class … Continue reading

Posted in Development | Tagged , , | Comments Off

CSS Progress Bar

How can you create a progress-bar with pure CSS, no images? In this post, we discuss one such attempt by Launchpad–where we have a target article-count goal, which we display in the dashboard. And we use a progress bar that’s pure CSS. Continue reading

Posted in Development | Tagged , | 1 Comment

String Replacement in Ruby

How can you replace strings in Ruby? There are a couple of options; there’s the tr function, which takes two sets of characters, and substitutes them; and there’s the gsub method, which makes a global replacement based on a regular expression. Powerful stuff! Continue reading

Posted in Development | Tagged , , | Comments Off

Ruby Command-Line Arguments

How can you access command-line arguments in Ruby? Through the special array called ARGV. It’s a zero-indexed array of your command-line arguments; so ARGV[0] is the first, ARGV[1] is the second, and so on. You can use ARGV.length to get the number of arguments, too. Continue reading

Posted in Development | Tagged , , | 1 Comment

Rails 2.1: Increment and Decrement Value

Prior to Rails 2.1, the increment and decrement functions needed to be called multiple times if you wanted to increment/decrement by more than one. In Rails 2.1, you can pass in a second parameter: a value, how much to increment or decrement the field by. (And it can take negative values.) Continue reading

Posted in Development | Tagged , | Comments Off

Chaining Array Appends With <<

In Ruby, you can push an element to an array using the << method. This method returns the array, so you can chain appends using it, like so: a << "One" << "Two". Nice! Continue reading

Posted in Development | Tagged , , | Comments Off

Rails 2.1: Aggregate Expressions

Rails 2.1 allows you to perform complex aggregate queries, such as Donation.sum(“amount * 3″). This makes complex calculations a snap! Rails provides functions for calculating the average, count, maximum, minimum, and the sum. Continue reading

Posted in Development | Tagged , , | Comments Off

Rails 2.1: Named Scope

Rails 2.1 introduces something called named scopes; named scopes allow you to create named collections on a class-level for your models, such as Article.published. You can chain named scopes together, and even create parameterizable scopes by using the lambda keyword; you can pass in variables, of a sort, and Rails will plug the values into your named scope queries. Continue reading

Posted in Development | Tagged , , | 4 Comments

Rails 2.1: The First and the Last

Rails always made it easy to grab the first item in any ActiveRecord model. Rails 2.1 allows you to easily grab the last element, too, with the syntax ActiveRecord.find(:last). Like :first, you can add limits, conditions, etc. to it. Additionally, there are new static methods to allow you to call ActiveRecord.first and ActiveRecord.last to get the first and last elements respectively. Yay! Continue reading

Posted in Development | Tagged , , | Comments Off

Exception and Error Handling

A lot of programming languages have an error-catching (or exception-handling) mechanism, where you can trap errors. How do we do this in Ruby? There are two keywords: begin, and rescue; and the ancestor of all errors is called StandardError. Continue reading

Posted in Development | Tagged , , | 1 Comment