[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: ANN: MetaTags 1.0

Dave Thomas

9/9/2003 2:17:00 PM


On Tuesday, September 9, 2003, at 08:59 AM, Ben Giddings wrote:

> On Monday, September 8, 2003, at 11:59 PM, Dave Thomas wrote:
>> Then I could get the effect of literals using:
>>
>> c = Caml "one hump or two"
>
> Ack. I really don't like this. It is 4 keystrokes away from what
> looks like a standard constructor:
>
> c = Caml.new "one hump or two"
>
In some cases, yes But say we're looking at a date (ignoring current
implementations in the library).

The class constructor might be

def initialize(year, month, day)
...
end

It expects three integers.

We could still define the global method

Date "date string"

Which parsed the string, reporting errors for incorrect strings, and
then called the constructor with the three integers.

This approach also allows for memoizing the returned values, which in
general you probably shouldn't do with raw constructors.


Cheers


Dave


4 Answers

Aria Stewart

9/9/2003 3:50:00 PM

0

> Which parsed the string, reporting errors for incorrect strings, and
> then called the constructor with the three integers.
>
> This approach also allows for memoizing the returned values, which in
> general you probably shouldn''t do with raw constructors.

Wow... I really like that, Dave.

It also doesn''t pollute the namespace (much|at all), since it''s
occupying the Class name in the method namespace. I really like that --
it seems elegant, syntactically, at least.

It makes the YAML %{...} look really nice, too. Far nicer than a
here-doc, anyway, which I''ve never been able to indent properly.

Ari

Ben Giddings

9/9/2003 4:18:00 PM

0

Dave Thomas wrote:
> We could still define the global method
>
> Date "date string"
>
> Which parsed the string, reporting errors for incorrect strings, and
> then called the constructor with the three integers.
>
> This approach also allows for memoizing the returned values, which in
> general you probably shouldn''t do with raw constructors.

Hmm... I think it might be a better idea to create a "memoizer factory"
or something. Maybe instead of Date.new you''d say Date.build or
Date.construct, and instead of passing it a string, you pass it a "memo
string" or something to indicate it makes sense to memoize the operation.

You could do this by simply having a ''date_memoizer.rb'' file with
contents like:

class Date
def self.construct(arg)
@@memo_hash ||= Hash.new

if arg.respond_to(:memoizable)
...
else
...
end
end
end

The main problems I see are that:
* Date() looks like a constructor for a Date object, if you''re not
familliar with Ruby''s constructor syntax, and for anybody reading the
code that could be a bad thing. It might seem like there are two kinds
of constructors in Ruby, one for initializing from data, one for
initializing from strings.
* It''s not obvious that Date() has any connection to memoization
* I have an aversion to top-level functions. ;)

Ben


Marcin 'Qrczak' Kowalczyk

9/14/2003 8:33:00 AM

0

Dnia wto 9. wrze?nia 2003 18:17, Ben Giddings napisa3:

> * I have an aversion to top-level functions. ;)

Why your aversion to top-level functions is stronger than your aversion
to top-level classes?

--
__("< Marcin Kowalczyk
\__/ qrczak@knm.org.pl
^^ http://qrnik.knm.org.p...


Gavin Sinclair

9/14/2003 11:32:00 AM

0

On Sunday, September 14, 2003, 6:33:17 PM, Marcin wrote:

> Dnia wto 9. wrze?nia 2003 18:17, Ben Giddings napisa3:

>> * I have an aversion to top-level functions. ;)

> Why your aversion to top-level functions is stronger than your aversion
> to top-level classes?

A reasonable answer might be: "if I''m going to risk a naming
collision, I''d better get more than one function point out of it."

Gavin