Friday, August 31, 2007

Ruby: Adding a "not" method for readability

The other day I was working with an if statement that looked similar to the snippet below.

if !response.incomplete? && !response.invalid? && response.total > 0
...
end

I spent enough time in C, C#, etc to be able to parse the if statement fairly easily; however, I thought it would improve readability if I could write the following snippet instead.

if response.not.incomplete? && response.not.invalid? && response.total > 0
...
end

Adding a "not" method to Object turned out to be fairly easy. I started with the following tests (block syntax provided by dust).

require 'rubygems'
require 'test/unit'
require 'dust'

unit_tests do
test "not negates true to false" do
assert_equal false, nil.not.nil?
end

test "not negates false to true" do
assert_equal true, Object.new.not.nil?
end
end

The implementation of the "not" method is similar to the implementation of the "as" method provided by facets (an explanation of the "as" implementation can be found in a previous entry).

class Object
define_method :not do
Not.new(self)
end

class Not
private *instance_methods.select { |m| m !~ /(^__|^\W|^binding$)/ }

def initialize(subject)
@subject = subject
end

def method_missing(sym, *args, &blk)
!@subject.send(sym,*args,&blk)
end
end
end

The Object.not method returns an instance of Object::Not. The Object::Not instance is initialized with the subject (the instance that was sent the "not" message). The Object::Not instances are basically proxies that send all calls back to the original instance. The Object::Not class privatizes almost all of it's methods so that most method calls will be handled by method_missing. The method_missing implementation simply forwards on any method call to the subject and negates the result.

Sunday, August 26, 2007

Ruby: Calling methods of a specific ancestor

Following the recent post, State pattern using Modules and Facets, Aman King asked:
what happens when two modules are included in a class[?] ... [will included modules overwrite] any methods that were included already by an earlier included module[?] (for the full comment please see the referenced post)
In Aman's comment he also points out that the Programming Ruby provides the following information.
If a module is included within a class definition, the module's constants, class variables, and instance methods are effectively bundled into an anonymous (and inaccessible) superclass for that class. In particular, objects of the class will respond to messages sent to the module's instance methods.
Part of the answer to Aman's question is in the statement from Programming Ruby. The way I think of it, each class can have zero or one superclass; however, each class may also have zero or many ancestors that are proxies to modules. You could simplify the previous statement and think of the modules themselves being an ancestor, but it can be important to note the difference because a change to a module will be reflected by all classes that include that module (even classes that included the module before the new behavior was added to the module).

Let's look at an example of a classes' ancestors.

module James
end

module Lynn
end

class FamilyMember
include James
include Lynn
end

FamilyMember.ancestors # => [FamilyMember, Lynn, James, Object, Kernel]
FamilyMember.superclass # => Object

The ancestors collection includes the class itself [FamilyMember], all included modules [Lynn, James, Kernel], and the superclass [Object]. The order of the ancestors collection is also important. The order of the ancestors is the order that the methods will be looked up when the object receives a message. Therefore, any message that is sent to a FamilyMember instance will first look in the methods of FamilyMember, then in Lynn, then James, etc.

If Lynn and James were to define a method, both of those methods would live on the proxies themselves, not on the FamilyMember class. Since the methods live on the proxies, including more modules will not overwrite a previous method definition; however, the last included module to define a method will be the first consulted when that message is sent. The module that was included last (and defines the method) will execute and return, and any other definitions of that method (found on other ancestors) will not be executed.

module James
def name
"James"
end
end

module Lynn
def name
"Lynn"
end
end

class FamilyMember
include James
include Lynn
end

FamilyMember.ancestors # => [FamilyMember, Lynn, James, Object, Kernel]
FamilyMember.new.name # => "Lynn"

So, given the above, how does Kernel.as allow me to call the methods of James even though Lynn clearly has precedence? Let's look at the implementation:

# comments removed for the example, check out the source of Ruby Facets to see the comments

module Kernel
def as(ancestor, &blk)
@__as ||= {}
unless r = @__as[ancestor]
r = (@__as[ancestor] = As.new(self, ancestor))
end
r.instance_eval(&blk) if block_given?
r
end
end

class As #:nodoc:
private *instance_methods.select { |m| m !~ /(^__|^\W|^binding$)/ }

def initialize(subject, ancestor)
@subject = subject
@ancestor = ancestor
end

def method_missing(sym, *args, &blk)
@ancestor.instance_method(sym).bind(@subject).call(*args,&blk)
end
end

For performance reasons (I assume), Kernel.as stores the As instance in a hash; however, for the purposes of our example the only thing worth noting is that Kernel.as returns an instance of the As class initialized with self and the ancestor. Generally, the As instance is returned and a method is immediately called on the As instance. If the As instance doesn't respond to the message it is sent, the method_missing method is invoked.

def method_missing(sym, *args, &blk)
@ancestor.instance_method(sym).bind(@subject).call(*args,&blk)
end

The above method_missing definition is what allows you to call a method on any ancestor. Let's start with an example and then walk through the method_missing definition to see how it works.

require 'rubygems'
require 'facets'

module James
def name
"James"
end
end

module Lynn
def name
"Lynn"
end
end

class FamilyMember
include James
include Lynn
end

FamilyMember.ancestors # => [FamilyMember, Lynn, James, Object, Kernel]
member = FamilyMember.new
member.name # => "Lynn"
member.as(James).name # => "James"

In the above example the member instance receives the message as which returns an instance of As initialized with the member instance and the module James (as the ancestor). Following the return of the As instance, it receives the name message. Since the As instance doesn't define name, method_missing is called passing in :name as the first argument (sym). Within method_missing, the ancestor (James) receives the message instance_method with the sym (:name) as the argument. The instance_method method will return the unbound method name from the ancestor (James). Next, method_missing binds the unbound method (name) to the subject (the member instance) and sends the call message (with arguments, which are empty in our example). When the unbound method executes bound to the subject it can access any state or behavior of the subject. In our example, the method merely returns "James"; however, the example from State pattern using Modules and Facets verifies that a method from the subject may be called from the unbound method when it is bound to the subject.

Friday, August 24, 2007

Importance of Time

Time is one of the most valuable resources.

Because I believe the above thesis, I value Agile because it strives to provide constant feedback. When given feedback, I can adjust for success (which saves me time), or fail faster (which also saves me time).

Mac OS-X: Disk Inventory X

I recently ran out of space on my MacBook Pro. Luckily, Pat Sarnacke pointed me at Disk Inventory X.
Disk Inventory X is a disk usage utility for Mac OS X 10.3 (and later). It shows the sizes of files and folders in a special graphical way called "treemaps".

If you've ever wondered where all your disk space has gone, Disk Inventory X will help you to answer this question.
Disk Inventory X was exactly what I needed, and I highly recommend it if you are wondering where your space is going.

Thursday, August 23, 2007

Ruby: State pattern using Modules and Facets

While browsing the docs for Facets I noticed the Kernel.as method. Then, a day later I needed a solution that added behavior using the state pattern; however, it was more elegant for our solution if the state instances could call methods on the class containing the state object. Our first solution was to delegate to the containing class via the method_missing method of the state instances. While this did work, debugging became problematic when things went wrong.

I did a quick spike to see if Kernel.as could provide a solution.

require 'rubygems'
require 'facets'

module James
protected

def name
"James #{last_name}"
end
end

module Lynn
protected

def name
"Lynn #{last_name}"
end
end

class FamilyMember
attr_accessor :person
include James
include Lynn

def name
as(person).name
end

def last_name
"Holbrook"
end
end


member = FamilyMember.new
member.person = James
member.name # => "James Holbrook"
member.person = Lynn
member.name # => "Lynn Holbrook"
member.first_name rescue "first_name is not defined" # => "first_name is not defined"

There's a few interesting things about this solution. I can change the behavior at runtime by changing the person attribute. Also, the behavior is actually on the class instead of living on an independent state object that delegates back to the class. Lastly, each method is protected so they must be accessed via the delegations, and an error will occur if they are accessed directly.

Ruby: Facets

The first time I stumbled upon Facets was on Mauricio Fernandez's hiki. I (stupidly) mentally bookmarked it, but never bothered to make it back.

Fortunately, I recently got an IM from Pat Farley letting me know that I was missing out.
Ruby Facets is the single largest collection of core extension methods and standard library additions available for the Ruby programming language. --facets.rubyforge.org
If you haven't looked at Facets, you are missing out. I suggest getting familiar with it by browsing the API one class at a time.

Saturday, August 18, 2007

Ruby: initialize_with

In April I wrote about assigning instance variables in a constructor. The code I provided with that entry supports initializing your classes positionally or with a hash.

Since then, I've been thinking that a nice alternative might be a constructor that takes one argument without a name, but any additional arguments must be named (via a hash). Yes, I basically stole this concept from Smalltalk.

class Person
initialize_with :employee_id, :first_name, :last_name
attr_reader :employee_id, :first_name, :last_name
end

class Money
initialize_with :amount
attr_reader :amount
end

Below is a solution to making the above code execute as expected.

class Module
def initialize_with(*args)
first_arg = args.shift
define_method :initialize do |*arg|
instance_variable_set "@#{first_arg}", arg.shift
required_args = args.inject(first_arg.to_s) { |result, attribute| result << ", #{attribute}" }
raise ArgumentError.new("initialize requires #{required_args}") if args.any? && arg.empty?
args.each do |attribute|
raise ArgumentError.new("initialize requires #{required_args}") unless arg.first.has_key?(attribute)
instance_variable_set "@#{attribute}", arg.first[attribute]
end
end
end
end

One other small change that you might notice is that I've made all the arguments required. That's a preference that I might blog about later, but I think it should be easy enough to remove the raises if you want your options arguments to be optional.

For those interested, here's the code with tests.

class Module
def initialize_with(*args)
first_arg = args.shift
define_method :initialize do |*arg|
instance_variable_set "@#{first_arg}", arg.shift
required_args = args.inject(first_arg.to_s) { |result, attribute| result << ", #{attribute}" }
raise ArgumentError.new("initialize requires #{required_args}") if args.any? && arg.empty?
args.each do |attribute|
raise ArgumentError.new("initialize requires #{required_args}") unless arg.first.has_key?(attribute)
instance_variable_set "@#{attribute}", arg.first[attribute]
end
end
end
end

require 'test/unit'
require 'rubygems'
require 'dust'

class Person
initialize_with :employee_id, :first_name, :last_name
attr_reader :employee_id, :first_name, :last_name
end

class Money
initialize_with :amount
attr_reader :amount
end

unit_tests do
test "verify Person requires all options" do
assert_raises ArgumentError do
Person.new(2, :first_name => 'mike')
end
end

test "verify Person requires first arg" do
assert_raises ArgumentError do
Person.new(:first_name => 'mike', :last_name => 'ward')
end
end

test "verify Person employee_id" do
mike = Person.new(2, :first_name => 'mike', :last_name => 'ward')
assert_equal 2, mike.employee_id
end

test "verify Person first_name" do
mike = Person.new(2, :first_name => 'mike', :last_name => 'ward')
assert_equal 'mike', mike.first_name
end

test "verify Person last_name" do
mike = Person.new(2, :first_name => 'mike', :last_name => 'ward')
assert_equal 'ward', mike.last_name
end

test "verify Money amount" do
money = Money.new(10)
assert_equal 10, money.amount
end
end