How to use mdfind to search for every file on OS X

Spotlight is great, but it has some mom-friendly rules that limit the results. Since I'm a power user, I typically want to search the entire computer and see all results. Therefore, I use the mdfind command to search OS X.

Just pop open Terminal and type:

mdfind "income tax"

This will search for all files with the metadata words "income tax."

Solution to the "Coin Maker" computer science interview question, using Ruby

A common computer science interview question goes something like this:

"Write a program that makes change with a minimum amount of coins for a given amount -- a 'Coin Maker', if you will. You have unlimited amount of 1¢, 5¢, 10¢, and 25¢ coins. For example, $2.16 would consist of 8 quarters, 1 dime, 1 nickle, and 1 penny: 11 total coins."


How to get and set model data from inside a Rails model

How to get the my_var model variable:

puts self.var


puts var

How to set the my_var model variable:

self.var = "new value"

Note that when setting, you must prefix with self., unlike when getting. Also note that @var will not work for setting or getting model data, ever.

It's okay to make non-RESTful controllers & routes in Rails

As part of my ongoing professional education, I've decided to read through several Ruby on Rails projects on Github. It has been a great experience, and I am learning a lot. It's insightful to see where I'm better than other software architects, and where I have room to grow -- there are some very nuanced lessons to learn, and some very simple ones. This is one of the very simple ones.


Vagrant cheat sheet

Developing robust software requires an accurate testing and staging environment. Vagrant provides just that. It lets you mimic your production server map on your computer.


How to bind Vagrant to port 80 on OS X

First, stop Apache. Apache listens on port 80. If it's running, Vagrant won't be able to bind a VM to listen on port 80.

Then, just add a forwarded port to your Vagrantfile: "forwarded_port", guest: 80, host: 80

And boot up Vagrant as sudo:

sudo vagrant up

:dependent => :destroy vs. :dependent => :delete_all in Rails models

  • If you use :dependent => :destroy in your Rails model, each model to be destroyed will be instantiated and the destroy() method will be called upon it. This will result in the normal object destruction lifecycle, include any callbacks. Destroying children and grandchildren can be handled gracefully. It is a safe, normal way to destroy objects. If you're not sure whether to use :dependent => :destroy or :dependent => :delete_all, use :dependent => destroy.
  • If you use :dependent => :delete_all in your Rails model, no objects will be loaded into memory and no destroy() methods will be called. Instead, a simple SQL query along the lines of DELETE * FROM dependent_model where depending_model_id = N will be run. There is no concern paid to destroying children and grandchildren; they will be orphaned in the database. If constructing a dependend model object and invoking destroy() is slow, or you simply wish to bypass the normal object destruction lifecycle, this is your best choice.

Rails' link_to always gives me a headache

After 9 years of writing Ruby on Rails code, link_to() never ceases to trip me up. Does it trip you up, too? Here are some quick notes:

  1. The first argument is what you want inside the <a> tag. For example link_to("today's news") produces <a>today's news</a>
  2. The second argument is the href attribute value. For example, link_to("today's news", "") produces <a href="">today's news</a>
  3. The third argument is a hash that contains everything else -- most importantly, your <a> tag attributes. For example, link_to("today's news", "", {id: "newsbox", class: "infos"}) produces <a id="newsbox" class="infos" href="">today's news</a>. The third argument can contain many other options, of which I have never been able to find a definitive list.

How to start and stop Apache in OS X

How to start Apache:

sudo apachectl start

How to stop Apache:

sudo apachectl stop

How to restart Apache:

sudo apachectl restart

N.B. I am on OS X Mavericks. It ships with Apache 2.

The Ruby on Rails folder structure

When a new Ruby on Rails project is created with rails new, many files and folders are created inside your project folder. Here's a quick reference of what those are.


How to assign a variable based on a condition in Ruby

If you need to assign a variable based on a conditional, most programmers would do something like this:

result = nil
if condition == true
  result = 1
  result = 2
puts "The result is {result}."

I usually cringe when people say things like, "Oh, that's a very C way of doing that"... but in this case, well, that's a very C way of doing that.


How to list all rake tasks

rake -T -A

This command will print out all available rake tasks. You can use it with Rails' Rakefile, or with a custom Rakefile. N.B.: not all tasks have descriptions, and that is okay.

How to delete all rows in a MySQL or Oracle table

There are two methods:

DELETE FROM table_name



The latter is more efficient, because it does not reference each row before deleting; it is a bulk operation. The former is expanded into DELETE * FROM table_name, which works on one row at a time. Therefore, use the latter.

Bash cheat sheet

Technically, this is a bash cheat sheet; however, many of the commands will work with Bourne shell descendants, such as zsh.


Using attr_accessor in Rails model classes

  • Don't confuse attr_accessor and attr_accessible -- they have very similar names
  • attr_accessor is a pure Ruby function to create reader and writer methods for an instance variable
  • attr_accessible is a pre-Rails 4 function to list the model attributes (e.g., database fields) that can be set via mass assignment, providing access control

You can use attr_accessor on a Rails model to create non-persisted instance variables. For example:

class Stock
  attr_accessor :current_price

This allows you to get the @current_price of a stock -- something that would make no sense to persist.

How to run Javascript code on Mac OS X

When developing Javascript, you may wish to run code outside the browser. Luckily OS X comes with a world-class Javascript engine, which features a command-line interpreter.

Since it's hidden deep inside a framework, create a symbolic link:

sudo ln -s /System/Library/Frameworks/JavaScriptCore.framework/Resources/jsc /usr/bin/js

Now, you can enter the interpreter by simply typing:


The interpreter can be quit by typing quit(); or ctrl+c.

tmux cheat sheet

Since switching to vim, I've found myself really enjoying keyboard-based user interfaces. So instead of managing multiple terminal sessions in iTerm, I have started using tmux. I'm writing this cheat sheet as I learn. Check back often.


Moving around in Linux with pushd, popd, and dirs

Often when administering a Linux server, you move around between the same few directories. You cd here, you cd there, and then you cd back here. Instead of cd'ing so much, Linux has 3 powerful commands that can help you: pushd, popd, and dirs.


My ssh config file template

If you ssh into servers, you must set up a .ssh/config file. It will allow you to type ssh myd instead of ssh -i ~/.ssh/mykey.pem -p 1234.


reverse-i-search'ing in the Linux shell

When you press ctrl+r in the shell, you activate a feature called "reverse-i-search." Reverse-i-search'ing lets you search through your shell history commands, from most recent to oldest.


How to put a prompt before gets.chomp in Ruby

Do you want to ask the user for input at the command line, using a slick 1980's hacker movie prompt like this: >>?

Don't use gets.chomp. Use readline instead:

require 'readline'

...will output:

>> I can write stuff here

VIM Cheat Sheet

After 9 years of TextMate, I'm switching to vim. I'm writing this cheat sheet as I learn. Check back often.


How to check if Bash is vulnerable to Heartbleed

Run this line in your Bash console:

env x='() { :;}; echo vulnerable' bash -c "only this line should appear"

If you see this, your Bash is vulnerable to Heartbleed:

only this line should appear

If you see this, your Bash is secure from Heartbleed:

only this line should appear

How to obtain the number of files in a folder, recursively, using Ruby

I have often found myself wanting to know how many files are in a folder, including files in sub-folders. I whipped up a little Ruby code for just that:

folder_to_count = "/path/to/folder"  # You should change this
  file_count = Dir.glob(File.join(folder_to_count, '**', '*')).select { |file| File.file?(file) }.count
  puts "ERROR: The number of files could not be obtained"
  # typically occurs if folder_to_count does not exist

How to make a ruby file executable

In the ruby file:

#!/usr/bin/env ruby
puts 'Hello world'

At the command line:

chmod +x ruby.rb

Then you can execute it like this:


CEVA (Amazon) phone number

(800) 592-7489

This is the number for CEVA, the company that delivers oversized / bulk items for Amazon.

I wasted 15 minutes trying to find their phone number.

Toggling two code blocks

Often when debugging code, you toggle between two blocks of code. You comment in one block, and comment out the other block. As simple as it sounds, going back and forth dozens of times can become quite annoying.


The best PGP tutorial for Mac OS X, ever

When I decided to set up my Mac with PGP encrypted communications, I could not believe how hard it was -- not just to set up the software, but to understand how to use PGP properly. There was no "PGP for Dummies" tutorial for OS X on the internet. So I decided to write one. This is my über simple, nerd-free tutorial for anyone on Mac. In it, I will:

  1. Cover exactly how to install and configure PGP on OS X
  2. Demonstrate how to use PGP in real life

How to dump all MySQL databases to a gzip file from inside a bash script

Often as part of an hourly, daily, or weekly server backup bash script, you will wish to dump all MySQL databases to disk as a gzip file. This way, in the case that any of your databases suffer from corruption or user error, you will have a backup.

Simply put this command inside your cron-scheduled backup bash script, substituting the USERNAME, PASSWORD, and the destination path with your info:

mysqldump -u USERNAME -pPASSWORD --all-databases --routines| gzip > /mysqlbackups/MySQLDB_`date '+%m-%d-%Y'`.sql.gz

Creating a ruby launchd task with RVM in OS X

In the last few years, ruby has taken the world by storm. With different projects dependent on different versions of Ruby, many Rails, Jekyll, and Sinatra programmers have installed RVM to manage their ruby versions in OS X. I am one of them.

While RVM is awesome, it can make some previously simple tasks difficult. One such example is creating a launchd daemon that invokes a ruby script. If your gems are installed with RVM, you will need to use RVM to write your daemon. That's where it gets complicated.

This tutorial teaches you how to schedule a ruby task on OS X using launchd. You can schedule any ruby file to be run. It will be executed using the RVM ruby version of your choice.


How to start and stop Apache 2 in Ubuntu Linux

How to start Apache 2:

sudo service apache2 start

How to stop Apache 2:

sudo service apache2 stop

How to restart Apache 2:

sudo service apache2 restart

How to reload Apache 2:

sudo service apache2 reload

Reloading is useful if you change a virtualhost file, since it will not actually stop Apache. Therefore, your websites will not go offline (even for a few seconds).

Where to put custom bash scripts in OS X

If you make a custom bash script in OS X, and you want everyone on the system to be able to access it, this is the directory where you should save it:


The percentage of people who are mentally unhealthy

This year I have been conducting research concerning the percentage of mentally unhealthy people for an upcoming project.

I am interested in:

  • Statistics about America, other countries, and global
  • Not just mental illnesses (Skitzophrenia, Bipolar disorder, Autism spectrum), but also less several mental unhealth (difficulty holding long-term relationships, not feeling fulfilled, phobias).
  • The precise definition of mental health and mental illness
  • Conducting primary research, if necessary

How to get the Ubuntu version from the commandline

lsb_release -a

Look for the line that starts with Release:. The following number is your version number. As of this writing, mine says Release: 12.04, which means I am running Ubuntu version 12.04.

How to get your Postfix version

postconf mail_version

You will see something like this: mail_version = 2.9.6. In this case, your Postfix version is 2.9.6.

How to show hidden files in Finder OS X

To show hidden files in Finder, type this into the Terminal:

defaults write AppleShowAllFiles TRUE
killall Finder

To set things back to normal, type this:

defaults write AppleShowAllFiles FALSE
killall Finder

How to change a line into a shape in Illustrator

In Illustrator, there are line objects and shape objects. Lines are made of points, and shapes are made of boundaries. Since line objects are not easily resized or manipulated, you will want to convert your line into a shape.


How to download Wordpress in Linux

Every time you set up a new Wordpress site in Linux, you will need to download a fresh copy of Wordpress. Here's the fastest way:

tar -xzvf latest.tar.gz

I love this recording and mastering style

Yesterday I found this recording of Rodelinda, Hwv 19: Act I - "Art Thou Troubled?" by G.F. Handel. I love the recording and mastering style the engineer used. The micing is obviously very close, and there is ample mastering going on -- definately some chorus, and probably some autotune. It sounds like some of the parts are doubled, maybe.

If I ran the Grammys, I would give them a recording award!

How to remove the dotted border from around select tags in Firefox

CSS form styling is an increasingly common design trend. If you do style your <form> elements, applying some CSS to a <select> tag can be really cool. Except in Firefox, which displays a rather annoying dotted border around the <select> tag.

Here's how to get rid of it:

select:-moz-focusring {
    color: transparent;
    text-shadow: 0 0 0 #000;

How to set up a new virtualhost domain in Apache 2

This guide will show you how to quickly set up a new website in Apache 2 using virtualhosts. Follow these instructions exactly as written, down to the character: trailing slashes, case, line breaks, etc.

Start by firing up a terminal, and creating a folder for the new website:

mkdir /var/www/

My minimal Apache2 virtualhost file

I have written at length about how administering a Linux server is a nightmare. It does not matter how smart, experienced, or nerdy you are. Everyone who uses Linux bangs their head against a wall from time to time.


How to change ownership recurisively in Linux

Run this command, replacing USER and GROUP with the user and group to which you would like the files changed; substitute DIRECTORY for the folder whose ownership you would like to change:


How to open the MySQL console

The MySQL console allows you to directly execute SQL commands, without having to use a programming language adapter such as PHP, Ruby, or Python.

Run this command, replacing USERNAME with your MySQL username; you will be prompted for the password.

mysql -u USERNAME -p -h localhost

How to unzip a file on the Linux commandline

In order for this to work, the unzip command must be installed. If you do not have unzip on your system, install it using the package manager for your distro (apt-get, etc.).

unzip ./

How to dump a MySQL database

There are many cases where you will want to dump a MySQL database. Examples include: backing up a website, migrating a website to a new server, moving from a development machine to a production server, and more.

Simply run this command, substituting the USERNAME, DATABASE_NAME, and FILENAME with your info. When you run this command, it will prompt you for the MySQL password for the USERNAME you provided: