[REBOL] Re: Rebol vs Ruby
From: chaz:innocent at: 20-Dec-2000 23:16
I'm trying to figure out how to implement what I think that code does.
Unfortuately I get an error.
It looks like it's trying to parse a series of blocks. The blocks are in
the form of units of time measured in seconds, singular form of the unit,
and the plural form of the unit.
> table.each {|unit, sing, plur|
> plur = sing+'s' if !plur;
If the plural is absent, we assume that it is formed by adding s to the
singular form.
For instance the plural of "year" is "years", but perhaps, as in some
contrived situation like this, the plurals cannot be formed this way.
table: [
[31557816 "year" ]
[2629818 "month" "rewarding months"]
[86400 "day" ]
[3600 "hour" "enjoyable hours"]
[60 "minute" ]
[1 "second" ]
]
time: 2629818 + 2629818 + 2629818 + 3600 + 3600 + 15
; I expect the output "3 rewarding months 2 enjoyable hours
; 15 seconds"
result: ""
rule: [
set unit integer!
set sing string!
set plur [none! | string!]
]
foreach line table [
; parse returns false if there is no plural entry
if not [parse line rule] [plur: rejoin [sing "s"]]
; Unfortunately the error occurs here
size: time / unit
; We want to take the integer part of size and compare it to 1 to
; determine if we need to use the singular or plural form.
if [to-integer size > 0][result: rejoin [result size " " either [size 1][sing][plur]]
" "]
time: time // unit
]
print result
chaz
At 01:25 PM 12/19/00 -0800, you wrote: