HowTo : sub.(regexp,$1)

  • Thread starter Bermejo, Rodrigo
  • Start date
B

Bermejo, Rodrigo

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.
 
M

Mark

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
 
M

Mauricio Fernández

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 }


--
_ _
| |__ __ _| |_ ___ _ __ ___ __ _ _ __
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
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
 
R

Robert Klemme

Mauricio Fernández said:
"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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top