Ruby tips: Procs vs lambdas

14 May 2013

I recently ran into a Ruby subtlety that I had heard of but never encountered before, regarding the use of the return keyword within blocks. Let's jump right into an example. Let's say you have an array of numbers and you want to select all the even ones. What will the following output?

def evensWithReturn(items)
  items.select do |item|
    if item.even?
      return true
    else
      return false
    end
  end
end

> evensWithReturn([1,2,3,4,5,6])
# => false
Full Post »

Faking keyword arguments in Ruby

9 May 2013

With the official release of Ruby 2.0, there have been a myriad of posts about all of its shiny new features. Of those, my personal favorite is the addition of keyword arguments. This is essentially built-in support for what's been traditionally accomplished in Ruby using options hashes. For example, it's not uncommon to see method calls like foo(bar: "one", baz: "two"). However, under the hood this might be implemented as:

def foo(options={})
  bar = options.delete(:bar)
  baz = options.delete(:baz)
  puts "#{bar} #{baz}"
end

foo(bar: "one", baz: "two") # => "one two"
Full Post »

Git tricks: Unstaging files

6 May 2013

This post dives a bit into the git reset command. If you want to jump straight to the good stuff, click here.

I wanted to share a handy alias I use for removing files from the staging area in git. Often I'll be working and adding files to the staging area with git add, and then decide (for example), that I don't want to commit some files with the others and get them out of the staging area (but keep my work intact). Let's look at an example case - running git status after staging a file might look like this:

$ git add example.txt
$ git status
# On branch test
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#   modified:   example.txt
#
Full Post »

Naked asterisk parameters in Ruby

10 Apr 2013

I came across a Ruby idiom I hadn't seen before browing through some of the Rails source the other day. Specifically, the method in question looked like this:

def save(*)
    create_or_update
rescue ActiveRecord::RecordInvalid
    false
 end

I've never seen the asterisk as a parameter by itself, and to explain it a brief review of the splat operator is in order. The splat operator allows methods in Ruby to accept a variable number of arguments. For example:

def say_hello(*people)
  people.each { |person| puts "Hello #{person}!" }
end
 
say_hello("Alice", "Bob", "John")
# "Hello Alice!"
# "Hello Bob!"
# "Hello John!"
Full Post »

A gem for managing your code notes

14 Jan 2013

I just released my first gem to the world, a little command line tool for managing source code annotations. I found myself constantly scattering little notes such as TODO, OPTIMIZE, etc into my code comments, and I wanted a way to quickly view all such notes in my project directories. The result, the Notes-CLI gem, packages an executable for recursively searching through source files for common annotations with the flexibility to add your own search terms. If you've ever worked with Rails, this is very similar to the rake notes command, extracted to work with any project.

The gem installs the notes executable, which can be run with -h for some more information. For an example, if I run the notes command in one of my project directories (a Rails app), I get something similar to the following:

$ notes app/    # Only search in the app/ directory

app/models/user.rb:
  ln 32: # OPTIMIZE: this method can be cleaner
  ln 34: # TODO: this parameter is a hack

app/views/expenses/_net_total.html.erb:
  ln 1: <%# TODO: condense this eventually  %>

The options for adding custom search terms or excluding directories from search are documented on the project's Github page. If you want to jump right in, just run gem install notes-cli.

This project started as a tiny script with a bash alias. Converting it into a gem for distribution turned out to be a trivial process, thanks to this article. I've found it to be super useful, and I hope someone else is able to get some value out of it. Give it a shot and let me know what you think!

gemruby