HowTo : sub.(regexp,$1)

Dear Ruby List:

I’m trying to add one hr, to some time strings:

“1:12:00” --> “2:12:00” (i don’t need to worry about 24)

Mi first try was …

“1:12:00”.gsub(/(^\d?\w)/,$1.to_i.succ.to_s)

Now I know that if I do this, at the time of the replacement $1 will be
empty.

could you illuminate myself with some ruby/intelligent way to achieve this ?

Thanks.

Bermejo, Rodrigo wrote:

Dear Ruby List:

I’m trying to add one hr, to some time strings:

“1:12:00” → “2:12:00” (i don’t need to worry about 24)

One way is to use the block form of gsub

Mi first try was …

“1:12:00”.gsub(/(^\d?\w)/,$1.to_i.succ.to_s)

instead use

“1:12:00”.gsub(/(^\d\w)/) {$1.to_i.succ.to_s)

which gives

“2:12:00”

HTH

Best Regards

Mark Sparshatt

“1:12:00”.gsub(/(^\d?\w)/) { $1.to_i.succ.to_s }

···

On Sat, Feb 07, 2004 at 04:50:21AM +0900, Bermejo, Rodrigo wrote:

Dear Ruby List:

I’m trying to add one hr, to some time strings:

“1:12:00” → “2:12:00” (i don’t need to worry about 24)

Mi first try was …

“1:12:00”.gsub(/(^\d?\w)/,$1.to_i.succ.to_s)


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

‘Ooohh… “FreeBSD is faster over loopback, when compared to Linux
over the wire”. Film at 11.’
– Linus Torvalds

“Mauricio Fernández” batsman.geo@yahoo.com schrieb im Newsbeitrag
news:20040206212618.GA9696@student.ei.uni-stuttgart.de

···

On Sat, Feb 07, 2004 at 04:50:21AM +0900, Bermejo, Rodrigo wrote:

Dear Ruby List:

I’m trying to add one hr, to some time strings:

“1:12:00” → “2:12:00” (i don’t need to worry about 24)

Mi first try was …

“1:12:00”.gsub(/(^\d?\w)/,$1.to_i.succ.to_s)

“1:12:00”.gsub(/(^\d?\w)/) { $1.to_i.succ.to_s }

I’d prefer a solution that works without a global variable:

“1:12:00”.gsub(/^\d+/) {|m| m.to_i.succ}

Regards

robert