Hi --

On Fri, 21 Jan 2005, Richard Turner wrote:

> Hi,
>
> I'm new to Ruby

Welcome!

> , so I'm still learning the Ruby Way.  I'm also reading
> Martin Fowler's 'Refactoring' at the moment and have realised that some
> of the classes I've created in a program I'm writing fit his description
> of value objects that should be refactored into reference objects.
> Those classes are, in fact, wrappers over entities in a DB so I need a
> factory (creation) method to always return the same object when given
> the same creation parameter.  E.g.:
>
> Section.getSection(10) always returns the same object representing the
> record in the DB with primary key '10'.
>
> Since I can't make Section's constructor private in Ruby I wonder how
> should I refactor my class into a reference object class?  Indeed,
> should I do this or does the Ruby Way use some other method?

At the risk of over-simplifying, let me try to simplify :-)

Generally in Ruby you don't need or see too many methods with 'get'
and 'set' in their names, since there are a variety of ways to perform
those operations less verbosely.  For example, you can define a method
called [] and then use index-style bracket syntax:

   class Section
     def self.[](n)
       # here, a fetch command for record n
     end
   end

   record = Section[10]

That would bypass the need for a constructor (if that's appropriate in
your case).


David

-- 
David A. Black
dblack / wobblini.net