September 2007

Cool Erlang like feature in Ruby

So, in joining the bandwagon, I’ve been reading about Erlang recently.

One of the features I really like is matchers:

    1 many(X) ->
    2   case X of
    3     [] ->
    4       none;
    5     [ _One ] ->
    6       one;
    7     [ _One, _Two ] ->
    8       two;
    9     [ _One, _Two, _Three |_Tail ] ->
   10       many
   11   end.

This code will respond none for an argument array of no elements, 1 for 1 and so on. The two parts of this I like are a) the fact that Erlang matches against the number of variables, without extra syntax, and b) the fact that the underscore variables are discarded.

I just found out that you can discard variables in a parallel assignment in Ruby too!!!

    1 name, _, ext = first.name.spec.split(.)
    2 name # => “first”
    3 ext # => “spec”
    4 

That my friends, is awesome, and could only be made better by allowing me to splat the _ (*_) to allow it to catch an arbitrary number of variables.

Enjoy!

Uncategorized

Comments (0)

Permalink

Vacation

I’ll be on vacation in Lousiana for the next week. See ya in October.

Aside

Comments (0)

Permalink

ActionPack: Filter types

Using an external class makes for more easily reused generic filters, such as output compression. External filter classes are implemented by having a static filter method on any class and then passing this class to the filter method. Example:

class OutputCompressionFilter
def self.filter(controller)
controller.response.body = compress(controller.response.body)
end
end

class NewspaperController < ActionController::Base
after_filter OutputCompressionFilter
end

The filter method is passed the controller instance and is hence granted access to all aspects of the controller and can manipulate them as it sees fit.

Aside
Ruby on Rails
coding

Comments (0)

Permalink

RSpec Presentation

This is a zip of my RSpec presentation from Desert Code Camp this past weekend.

It includes all of my slides in the PDF, plus all of the supporting code.

Enjoy!

RSpec
Ruby on Rails
Technology
coding
desertcodecamp

Comments (0)

Permalink

RSpec cheatsheet

I couldn’t remember where I got this from, so I am posting it here. If anyone can help me figure out where this comes from I would love to give proper credit.

RSpec Cheat Sheet

RSpec
Ruby on Rails
Technology
coding

Comments (0)

Permalink