Ruby opinions (111)

17 Name: dmpk2k!hinhT6kz2E 2005-12-03 03:37 ID:Heaven

Talking about which, explain to me why sub does two passes.

Okay, Ruby ripped of some ideas from Perl, including regex. GJ! Unfortunately, it didn't include the rather terse s/// syntacitic sugar. Fine, it's not pretty, but I can live with that. However:

i = '\\'
puts i.sub(/\\/, '\\')

The result is a single backslash. What about this:

i = '\\'
puts i.sub(/\\/, '\\\\')

Should give two, right? No, it gives one. If I want two, I need to use either '\\\\\\' or '\\\\\\\\'. Or I can use:

i = '\\'
puts i.sub(/\\/) { '\\\\' }

Least surprise? I think not.

Name: Link:
Leave these fields empty (spam trap):
More options...
Verification: