John Terry

writes code. plays drums. eats food.

Passing Objects to Methods as Blocks in Ruby

Lambda’s can come in handy when you need to apply a similar mutation to a collection of objects.

Here’s an example of a simple operation but the block can be as complicated or generic as you need.

Passing ruby lambda as a block
1
2
3
ints_to_floats = lambda { |i| i.to_f }
(0..5).map(&ints_to_floats)
#=> [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]

The urnary ampersand calls the :symbol’s #to_proc method and passes it as a block to the method.

In ruby 1.9.2 and monkey patched in by rails, Symbol#to_proc is defined as:

1
Proc.new { |*args| args.shift.__send__(self, *args) }

So our earlier exmaple can be replaced with

1
2
(0..5).map(&:to_f)
#=> [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]

So thats the trick behind that ruby magic.

Don’t Oversell

When you overpromise you only do a diservice to yourself and those that depend on you. And when you don’t meet your objective there is only guilt and frustration. Your colleagues won’t like you, you won’t like you, you won’t like what you’re doing, and you won’t want to do it anymore. This is the edge of the pit of despair. A slippery slope from being already behind and stressed to being late, working on something you don’t want to and more stressed that you were already. Thats the path to burn out. Once you begin training your brain to dislike the thing you’re doing you’ll become bored and frustrated with it.

Of course, passion can help motivate through the rough patches. But the most important thing to do is power on. Finish the task, or quit it. Finishing is far preferable, as it offers some validation that all of the miserable effort spent was not in vain. Quitting should only be considered when working on catching up is prohibiting you from keeping up with whats new. Part of my problem is that I am a picky about code design and I feel like nothing is ever quite perfect; that there is always room for improvement. And while, I also believe that is true, I know it is important to be moving forward. Nit picking is easy and you can chase perfection forever without going anywhere. Ship early and often is the software montra. Minimum viable product is another that is often referenced. It is more important to finish tasks to an acceptable level than to overly concern yourself with every minute detail.

Now I have to get back to finishing up this Ringswitch task I’m behind on.

Beginning 2012

Hello. Here we are again.

Welcome to 2012 and its time for me to update my blog. One of my ‘resolutions’ this year is to write regularly, and likely some of that will be in the form of blogging.

But I’ll also be posting things things that I am pondering or things I find interesting. I resolve to reflect on interesting things in my life to better understand interest.

This blog is built with Octopress and hosted out of an Amazon S3 bucket. Octopress is a “blogging engine” based on top of the same static page generator that Github uses for their pages. I put blogging engine in quotes because it is basically a composition of a bunch of ruby libraries, sensible defaults, and a nice theme. I don’t mean to put it down on that aspect, because it is precisely everything I want in a blog solution. As a web programmer I find that this solution is a perfect balance of being easy to post, extend, and produces totally universal output.

The only tricky part of the setup is that octopress doesn’t support S3 out of the box. For now I’m using Transmit to upload the site to my bucket, but this should be trivial to script. That’ll be the first improvement I make.

Check back tomorrow.