My Favorite Monkeys

F11EC193-AA43-498C-87BE-0023F5AD48E8.jpg
In whatever language I use, I wind up building up a collection of utility methods that I take from project to project. I think everybody does this in one way or another.

In Ruby, of course, you can monkey patch those methods right into the existing core classes... here are a few monkey patches that I can't do without.

One way to get yourself in trouble in Rails is to have some methods that expect ActiveRecord ids, and others that expect the actual ActiveRecord objects. It's easy to get into this if you start passing the id's directly from params sometimes and other times convert the param in your controller. So, I add this conversion to Integer and ActiveRecord::Base. The usage is as follows:

def do_something(ar_or_id)
ar_or_id.to_active_record(User).full_name
end

The implementation lets the object system handle type management. The only weird wart is that you have to pass the actual ActiveRecord class to the conversion method, I think that looks a little strange.

class Integer
def to_active_record(ar_class)
ar_class.find(self)
end

def to_active_record_id
self
end
end

module ActiveRecord
class Base
def to_active_record(ar_class)
self
end

def to_active_record_id
self.id
end
end
end

If you're passing the ids direction from params, you might also want to add the same two methods to String, both of which could defer to the Integer method.

Somehow Date always seems to collect extensions, it always seems like I need to do something just slightly off the beaten path. I like these, which convert a date to a larger range:

class Date
def to_week_range
sunday = self.beginning_of_week.yesterday
sunday .. (sunday + 7)
end

def to_month_range
self.beginning_of_month .. (1.month.since(self.beginning_of_month))
end
end

I find I always use these extension of Enumerable, which converts the list into a hash. In the first one, the existing list becomes the values of the array, in the second, the existing list is the keys. The other half of the Hash is generated from a block, which is a good way to create a quick mini-hash.

module Enumerable
def to_hash
result = {}
each do |i|
result[yield(i)] = i
end
result
end

def to_value_hash
result = {}
each do |i|
result[i] = yield(i)
end
return result
end

In use, it looks something like this:

>> [1, 2, 3].to_hash {|i| i**3}
=> {27=>3, 1=>1, 8=>2}
>> [1, 2, 3].to_value_hash {|i| i**3}
=> {1=>1, 2=>8, 3=>27}

I use the converse, taking a Hash and a block and returning a list, less frequently.

The String class winds up with a bunch of patches. This pair is in my book, although for some reason, I didn't include it in the String class there -- it's useful for random string tokens:

def self.random_string(len)
(1..len).map {String.alphanumeric_characters.rand}.join
end

def self.alphanumeric_characters
("A".."Z").to_a + ("a".."z").to_a + ("0".."9").to_a
end

I also use this one to convert various forms of param text to a truth value, it converts the strings "true", "t" or "1" to Ruby true:

def is_true?
%w(true t 1).include?(self.to_s.downcase)
end

Lastly, this is a simple flip of the include? method, because it sometimes reads awkwardly to have something like:

["student", "professor", "admin"].include?(user.role)

I think that reads backward, I prefer:

user.role.in?("student", "professor", "admin")

Which is simply implemented as:

class Object
def in?(*enumerable)
enumerable.flatten.include?(self)
end
end

The flatten lets you pass the arguments as either a normal parameter list (as in the above example) or as a literal array. It would cause problems if the receiver was actually an enumerable, though, you'd need to add another version of in? for Enumerable...

Topics:

Leave a comment

Powered by WP Hashcash

About Pathfinder

Follow the Blog

    Get a monthly update on best practices for delivering successful software.

    Subscribe via email

      

    Subscribe via RSS      RSS icon

Topics

Search

WordPress

Comments about this site: info@pathf.com