My Favorite Monkeys

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: Ruby on Rails
Leave a comment
About Pathfinder
Follow the Blog
-
Get a monthly update on best practices for delivering successful software.
Subscribe via email
Subscribe via RSS
Categories
Topics
Archives
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006
Blogroll
Recent
- Elements of Testing Style
- Aesthetics and Web Design
- Asterisk-Java Testing with Groovy
- 3 Misuses of Code Comments
- Fluently NHibernate
- Digging a Hole and Covering it with Leaves — The Software Development Version
- The Importance of User Experience - Do You Understand It in Your Bones?
- Writing Your Own Protocol With NSURLProtocol
- What’s In Your Dock: iPhone edition
- Feature Fatigue
