The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Selector Namespaces in Sydney

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Daniel Berger

Posts: 1383
Nickname: djberg96
Registered: Sep, 2004

Daniel Berger is a Ruby Programmer who also dabbles in C and Perl
Selector Namespaces in Sydney Posted: Oct 20, 2005 2:08 PM
Reply to this message Reply

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.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Daniel Berger
Latest Posts From Testing 1,2,3...

Advertisement
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.

Read: Selector Namespaces in Sydney

Topic: Universal Studios Previous Topic   Next Topic Topic: Ruby Conference Wrap-Up (Part 1)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use