On Monday, September 8, 2003, at 11:59  PM, Dave Thomas wrote:

>
>
> So, if I wrote a class called Caml, I'd also define a top-level 
> method, also called Caml, something like:
>
>    class Caml
>      def Caml.from_string(str)
>        # .. check string format ..
>        self.new(str)   # or something similar
>      end
>    end
>
>    def Caml(str)
>      Caml.from_string(str)
>    end
>
>

What about:

class Caml
   def Caml.from_str(str) # this aligns with to_str
     self.new(str) # or whatever
   end
end

Then:

class String
   def &(klass)
     klass.from_str(self)
   end
end

then you can do:

c = "one hump or two" & Caml

-rich

> Then I could get the effect of literals using:
>
>     c = Caml "one hump or two"
>
> If it was appropriate, the top-level method could be memoized, so we 
> could chose to have new objects created on each call or one object per 
> literal.
>
>
> Cheers
>
>
> Dave
>
>
>