This post originated from an RSS feed registered with Ruby Buzz
by Daniel Berger.
Original Post: Selector Namespaces in Sydney
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
A recent thread on selector namespaces came up on ruby-talk that I thought was interesting. For those who don't know what selector namespaces are, the short version is "encapsulation within scope". That might not make much sense immediately, so let's proceed to an example.
Let's say you want to have multiple methods with the same name within a class, with each one behaving differently:
class Foo
def length
10
end
def length
20
end
end
In Ruby 1.8.x, this is legal (though it emits a warning). However, you still only end up with one length method. The last one defined wins. How can we make it work? And how would it look?
Sydney uses Behaviors to accomplish this. It looks like this:
class Foo
def length
5
end
namespace :alpha do
def length
10
end
end
namespace :beta do
def length
20
end
end
end
The namespace keyword takes a block, and all method definitions within that block are assigned within the scope of the appropriate namespace, rather than within the scope of the class itself. So, let's see how it looks in action:
f = Foo.new
f.length # 5
namespace :alpha do
f.length # 10
end
namespace :beta do
f.length # 20
end
I'm not completely committed to this syntax. It's probably the easiest way to do things in lieu of Behaviors. A couple other ideas I (and others) have thought of are:
f{:alpha}.length # 10
f.length using :alpha # 10
with :alpha do f.length end # 10
None of them strike me as particularly compelling, however, and the current syntax doesn't look bad. It's just a bit verbose. Perhaps verbosity *should* be the price of selector namespaces.
Now, before the static/strong typing weenies chime in with snide remarks about selector namespaces being a workaround for Ruby's inability to do function overloading, consider this - I don't have to change the method signature. Besides, there's always Ryan Pavlik's strongtyping package if I want function overloading. :)
For the current implementation in Sydney, look here.