[ANN] MutableTime class

I’ve just finished my first general-purpose Ruby class, MutableTime.

You can read the RDocs for it at http://phrogz.net/RubyLibs/ (and you
can download it from the link at the bottom of the page for the
MutableTime.rb file)

What is MutableTime? It arose out of my desire for a Date/Time class in
Ruby that behaves like the Date object in JS. From the MutableTime docs:

···

-----------------------------------

The MutableTime class behaves much like the builtin Time class, except:

  • almost any property can be changed, e.g.
    mmTime = MutableTime.new
    mmTime.year = 1973
    mmTime.month += 13

  • by default, month numbers start at 0 instead of 1
    — see monthsStartAtZero?

  • you can choose if the first day of the week is Sunday or Monday
    — see weekStartsOnMonday?

  • it has convenience methods named similar to JavaScript’s Date object
    — fullYear, month, date, hours, minutes, seconds

  • customFormat is slightly more powerful than Time#strftime

  • you can easily internationalize the month and day names
    (without editing the source code for the class)

-----------------------------------

Please try it out and tell me what you think.

Also…what’s the standard protocol for releasing new classes like this?
Is there somewhere that I should upload the .rb file to? Somewhere other
than here that I should announce it?


(-, /\ / / //

RubyForge

···

On Sat, 14 Feb 2004 01:15:11 +0000, Gavin Kistner wrote:

Also…what’s the standard protocol for releasing new classes like this?
Is there somewhere that I should upload the .rb file to? Somewhere other
than here that I should announce it?

This is just a simple little stylistic query that I became curious about
after one of Ara Howard’s questions the other day.

In general, do people prefer:

def simpleExample(t)
x = block_meth do
r = nil
if t == 'forty-two’
r = 42
else
r = 0
end
r
end

x
end

or:

def simpleExample(t)
x = block_meth do
if t == 'forty-two’
r = 42
else
r = 0
end
end
end

both assuming this method is available:

def block_meth
yield
end

i.e. do you explicity declare a return variable and put that variable
right before the ‘end’ or do you rely on your audience being smart enough
to perceive what the return value is going to be?

On a really short piece of code like this, I appreciate the elegant
simplicity of not being explicit, though my person trend is to be explicit
anyway. On a longer piece of code, though, I can certainly see where it
might not be as clear to a reader of the code just what is being returned,
especially from a casual scan, if it is not done explicitly.

Is this clarity helpful, or is it a minor enough thing that the ugliness
that it imparts to the code isn’t worth it? What do you all think?

Kirk Haines

Hi –

This is just a simple little stylistic query that I became curious about
after one of Ara Howard’s questions the other day.

In general, do people prefer:

def simpleExample(t)

I prefer “simple_example” :slight_smile:

x = block_meth do
r = nil

You don’t actually need that in this case; an if clause does not start
a new scope.

if t == 'forty-two'
  r = 42
else
  r = 0
end
r

end

x
end

or:

def simpleExample(t)
x = block_meth do
if t == ‘forty-two’
r = 42
else
r = 0
end
end
end

Do you need “x =” or “r =” there at all? I think the all-out
implementation of this approach would just be:

def simple_example(t)
block_meth do
if t == ‘forty-two’
42
else
0
end
end
end

And, just for fun, there’s also:

def simple_example(t)
block_meth { if t == ‘forty-two’ then 42 else 0 end }
end

def simple_example(t)
block_meth { t == ‘forty-two’ ? 42 : 0 }
end

and even

def simple_example(t)
block_meth do
case t
when ‘forty-two’ then 42
else 0
end
end
end

(I won’t bother with the “t == ‘forty-two’ && 42 || 0” one :slight_smile:

both assuming this method is available:

def block_meth
yield
end

i.e. do you explicity declare a return variable and put that variable
right before the ‘end’ or do you rely on your audience being smart enough
to perceive what the return value is going to be?

On a really short piece of code like this, I appreciate the elegant
simplicity of not being explicit, though my person trend is to be explicit
anyway. On a longer piece of code, though, I can certainly see where it
might not be as clear to a reader of the code just what is being returned,
especially from a casual scan, if it is not done explicitly.

Is this clarity helpful, or is it a minor enough thing that the ugliness
that it imparts to the code isn’t worth it? What do you all think?

Personally I tend to like to avoid temporary variables and
accumulators in that situation, if possible. But I don’t think it’s
too ugly, usually, just a bit longer.

[Note: all code untested]

David

···

On Sun, 15 Feb 2004, Kirk Haines wrote:


David A. Black
dblack@wobblini.net

Kirk Haines wrote:

This is just a simple little stylistic query that I became curious about
after one of Ara Howard’s questions the other day.

In general, do people prefer:

def simpleExample(t)
x = block_meth do
r = nil
if t == ‘forty-two’
r = 42
else
r = 0
end
r
end

x
end

or:

def simpleExample(t)
x = block_meth do
if t == ‘forty-two’
r = 42
else
r = 0
end
end
end

I would do the following, which is more in the spirit of an expression-based
language:

def simpleExample(t)
block_meth do
if t == ‘forty-two’ then 42 else 0 end
end
end

Personally, I wouldn’t assign to variables that I’m not going to use.

···


Grzegorz Chrupała | http://pithekos.net
> grzegorzc@jabber.org

David A. Black said:

def simpleExample(t)

I prefer “simple_example” :slight_smile:

Amen!

And, just for fun, there’s also:

def simple_example(t)
block_meth { if t == ‘forty-two’ then 42 else 0 end }
end

def simple_example(t)
block_meth { t == ‘forty-two’ ? 42 : 0 }
end

I prefer either of these one-liners over the longer examples. They
succinctly express the heart of the calculation without any extra noise.

(I won’t bother with the “t == ‘forty-two’ && 42 || 0” one :slight_smile:

Good Heavens! Thank you. Where the other oneliners express the logic
directly, this one tends to cloud it (IMHO).

i.e. do you explicity declare a return variable and put that variable
right before the ‘end’ or do you rely on your audience being smart
enough to perceive what the return value is going to be?

On the short examples given, I would let Ruby be Ruby and just have the
last (and only) expression be the implicit result of the function.

If the function is longer (more than one expression), then I might use a
return variable, especially if the expression I wish to return is the last
expression only by coincidence. When I use a return variable, I always
spell it “result”, and only use the name “result” for return variables.

···


– Jim Weirich jim@weirichhouse.org http://onestepback.org

“Beware of bugs in the above code; I have only proved it correct,
not tried it.” – Donald Knuth (in a memo to Peter van Emde Boas)