InfiniteRed

art.through(code)

On off switch image

acts_as_boolean is a Ruby on Rails plugin I wrote that treats a column as a boolean, whether it’s a tinyint, integer, float, string, etc. No matter how true and false are stored in the database.

This is useful when you don’t have control over how booleans are stored by different applications. For example: Microsoft Access stores booleans as 0 and -1. Normally -1 in a MySQL database, would be converted, by Rails/ActiveRecord, into a false, rather than true as it should be.

You can use either foo (assuming your column is foo) or foo? methods and they return the same result.

The following are false (lower, upper, or any other case), all else are true:

false, nil, 0, 0.0, '', '0', '0.0', 'f', 'false', 'n', 'no', 'negative', [], {}

You can add to these, for individual columns, using the false_is_also parameter.
Note: if you use the set_false_as parameter, it will automatically be added to the list of falses for this column.

You can specify a specific way a boolean should be stored in the database, when it's assigned, with set_false_as and set_true_as. This, however, will not change how the column is evaluated on read. In other words, if you save a false as ‘nope’, a value of 0 or ‘f’ in the database will still be read as a false. It only affects how it is stored in the database.

Parameters

  • set_false_as – What false value to store in the column on assignment. By default any value will be stored, and converted to true or false when you read the value.
  • set_true_as – What true value to store in the column on assignment. By default any value will be stored, and converted to true or false when you read the value.
  • false_is_also – Additional values to be treated as false

Examples

The most simple example is when you want column foo to return true and false correctly for both foo and foo? methods. To do that, simply add the following to your model:

acts_as_boolean :foo

Some other examples:

class Person < ActiveRecord::Base
  acts_as_boolean :alive, :employee
  acts_as_boolean :vendor, :client, :set_true_as => -1
  acts_as_boolean :foo, :false_is_also => ['nine', 'nada', 9999]
  acts_as_boolean :bar, :set_false_as => 'no', :set_true_as => 'yes', :false_is_also => 'nyet'

  #...
end

Install

For Rails 2.1 and above:

ruby script/plugin install git://github.com/twerth/acts_as_boolean

To upgrade:

ruby script/plugin install git://github.com/twerth/acts_as_boolean.git --force

License

Released under the MIT license

Code Repository

The code repository can be found on github here. Happy coding.

Vim Logo

Some people in the comments of this article and this article requested my vimrc file. Rather than just providing it, I think it is better to check it into a source repository so people can get the latest version. You can get all my Vim files here:

GitHub Repository

...
Read entire article

Vim Logo
Color Scheme

This color scheme is based off of my popular IR_Black theme for TextMate.

When thinking of vim or vi, visually appealing UI doesn't normally enter your mind. But that isn't due to a lack of features, because its support for syntax coloring is one the best I've seen; the only thing slightly better is TextMate. It's due to the poor color schemes many people use.

...
Read entire article

It's common for artists, such as graphic designers, to keep scrapbooks of designs that catch their eye. This could be a logo, typeface, color scheme, page layout, or whatever else they deem of high quality. They use these scrapbooks (which traditionally were books with scraps of paper in them) to get inspiration, to help them develop their own style (by identifying likes and dislikes), or simply to browse.

Why does this matter to you, a developer?

That's a good question, and at first when I started to keep a scrapbook, I did it mainly for visual designs like web-pages. But once I started I realized it would work really well for diagrams, code, clever UI elements, or really any content that I think is exceptional.

If I'm working on a task, and feel bogged down and un-creative, I'll browse my scrapbook, getting inspiration. I look for little things that make the scraps great. I ask myself, why is this particularly good? This works just as well for a sort routine in code, as it does for logo design.

The main benefits of keeping a scrapbook are:

  • creating the habit of looking for, and identifying, content that you like, and
  • browsing the scrapbook at a later time to get your creative juices flowing
...
Read entire article

OS X Leopard was released with an updated Terminal application, which now has tabs, window groups, and many other new features. My excitement to replace the often slow iTerm was quickly extinguished as I realized that the new Terminal.app has some glaring problems:

  • The first is the inability to set the title of the tab as you do in iTerm, gnome-terminal, etc. That one I can live with, as there are work-arounds.
  • The other major problem is the horrible black themes that come with it (bad and worse). Apple is one of those companies who pay very close attention to visual details such as these, so it's surprising that they gave us such horrible choices.

So I decided to make a new theme, based on a subset of my popular TextMate theme IR_Black. The problem is the new Terminal app provides no way to set the ANSI colors; even though you can create your own themes (Settings), you can't change the colors. Ciarán Walsh provides a great solution to this on his blog, which uses the also great SIMBL.

...
Read entire article

To write a command-line application in Ruby is very simple, the following two-line application converts everything in the standard input to upper case and then outputs it:

#!/usr/bin/env ruby 
puts STDIN.read.upcase

Although complete, this is hardly a proper application, which should include options, arguments, help, input error trapping, etc. I've created a skeleton for such a command-line application.

...
Read entire article

Meaningless graphic

If you've been learning the command-line and you have the basics down (you should be, as the most effective way to use a computer is a combination of a GUI and command-line), the next step is to customize your environment.

The ability to fully customize your shell is one of the most powerful things about the command-line. It's a dry subject, and mastering it won't get you favors from the opposite sex (although it should), but it can be very useful.

There are many ways to customize your shell, but the first one you should learn is modifying your Bash startup files (assuming your shell is Bash, which is the default in OS X, Linux, and many other unices).

When I first learned how to customize bash, I found an overwhelming amount of information and opinion, which made it difficult. This article is intended to give you the fundamental concepts so that you can create your own startup files, and understand how they work. To give you an example, I go through a subset of my own files, section by section.

...
Read entire article
TextMate theme
Download Theme (once downloaded, simply double click on the file to install) I created the IR_Black theme, and some people asked for a white version of it. So instead of doing it manually I thought a quick Ruby script to make all the colors darker would work well. ...
Read entire article

TextMate theme
Download Theme (once downloaded, simply double click on the file to install)

This theme is based off of the Succulent and Brilliance Dull themes. I wanted a black theme that had different colors for a wide variety of items, with coordinated colors between like items.

...
Read entire article

Ruby OS X graphic

A fresh install of Mac OS X to Ruby on Rails, the right way, in a 28 minute screencast.

Many tutorials skip the little steps, causing people to get stuck. This screen-cast starts with a fresh install of Mac OS X Tiger (10.4.8), then goes step by step through the process of setting up a complete development environment for Ruby and Ruby on Rails.

I show you how to setup the following:

...
Read entire article