Archive for June, 2006

Friday, June 30th, 2006

Enumerable makes me giddy

I’ve been obsessing over the wonders of the Enumerable Module in Ruby for a little while now.

One of the first Ruby bits that I ever learned (but didn’t really grasp until recently) was using each() as a better iterator.

@books.each { |book| book.read }

which is more elegant than

for book in @books
book.read
end

once you understand and become |one| with the block.
Thats nice, but it wasn’t until I caught wind of the other Enumerable methods that my mind was blown.

I have a particular fondness for inject()

Building a space-delimited list of tags from an array of tags could be a relative clunk fest. Iterating over an array and adding the tags to the total list. My initial code was something like:

def tag_list
list = ""
tags.each do |skill|
list +=  skill.name + " "
end
list.strip
end

Thats not so bad. But something as simple as that should be able to fit on one line

def tag_list
tag.inject("") {|list,tag| list + tag.name + " " }.strip
end

inject() is a great tool once you realize the power of the shortcut. That goes for all the methods of Enumerable. The shortcuts are all based on the same idea of iterating over a collection and executing a block. The difference is what is returned out of the block and how the result is handled. For example, any?() iterates over a collection passing each element to a block and asserting whether the block returns true. If any block pass returns true, the function returns true.

The power of many of the Enumerable functions have been ported to JavaScript via the Prototype library. I’d say it should be brought to PHP, too, but that’s usually where the elegance train stops.

Friday, June 23rd, 2006

Cool as Ice

Italian Ices

Since I’ve returned to Brooklyn, I’ve re-established an old addiction, Italian Ices. There’s nothing like taking a short stroll to the neighborhood Pizzeria, Fiscati and walking home with your hands covered in sweet chocolate juices.

Brooklyn Ices in the Summer = The Best.

Friday, June 23rd, 2006

Leopard Shots (giggles with excitement)

Who knows if these screenshots of OS X 10.5 are real, but I just got really excited.

Tabbed Finder! Its what I’ve always wanted. I care way less about Boot Camp, since I don’t have an Intel. I’m all about UI improvements. Bring ‘em on, Stevie!

Tuesday, June 20th, 2006

Using the Basecamp PHP Wrapper

It’s been a little while since I’ve published my little wrapper for the Basecamp API. And though I’ve been playing around with it, I haven’t let anyone in on my little ideas. I was Gargamel to this PHP gold.

I feel like more than people wondering why you would want to use the Basecamp API with PHP, it’s more of a question of how to get started.

Well, lets get started.
First things first, lets use some Object Oriented magic to make our own library:

require 'basecamp.php';

class Customcamp extends Basecamp {

// our custom functions go here

}

By requiring and then extending the Basecamp class, we can add our own custom functions to the library without having to disturb the precious file. We can use our new ‘Customcamp’ by using it in the constructor:

require 'customcamp.php';

$camp = new Customcamp($user,$pass,$url);

Of course until we start adding functions to ‘Customcamp’, we’re not gaining anything over the original library. So let’s gain something.

class Customcamp extends Basecamp {

function load_projects_with_lists() {
$projects = $this->projects();
for ($i=0;$i < count($projects);$i++) {
$lists = $this->lists($projects[$i]->id);
$projects[$i]->lists = (is_array($lists))?$lists:array($lists);
}
return $projects;
}

}

This function is just a simple extension of the basic API calls. It pretty much does what is says - it returns an array of all the projects with every list in that project in an attribute called lists.

First we get an array of all the project objects:

$projects = $this->projects();

Then we iterate over the projects, getting all the lists for each one:

for ($i=0;$i < count($projects);$i++) {
$lists = $this->lists($projects[$i]->id);

The only line thats a little confusing is

$projects[$i]->lists = (is_array($lists))?$lists:array($lists);

All this is doing is making sure we always have an array of lists for each project. If there is only one list in a project, the API will only return the list instead of an array of one list. This quickie is useful for when we want to quickly iterate over the projects and then the lists as arrays of objects.

Well that was fun wasn’t it? Its really easy to add custom functions to the API so you can create quick calls on the Basecamp object itself instead of having to just use its results.

This is just the tip of the iceberg. I have a bunch more useful tidbits, but I’m going to save them for a (very) short while, so I can pull together a little demo.

Note: I know that 37Signals just added support for the time-tracking functions to the API. Unfortunately, my Basecamp account isn’t up to that level, so I can’t test any of that goodness. If anyone who can test that wants to add that functionality to the wrapper, let me know.

Sunday, June 18th, 2006

The Mobius Band at The East River

I spent Saturday afternoon hanging out with a couple friends and seeing some great (free) live music in the city. I went mainly for Mobius Band who are an awesome electro/indie/unclassifiable band that I’ve been following for a little while.

Their debut full-length, The Loving Sounds of Static is awesome - but personally I think their previous EP, City vs. Country is their pinnacle.

It was a great time and though it was really hot, it was a lot of fun to be baking in the heat to the sounds of good live music. I took pictures, but someone else took much better ones.