What's simplest way to turn the string "YingYang" into "Ying/Yang"?
+1
A:
Assuming you want to insert a slash everywhere where a lower case letter is followed by a capital letter:
"YingYang".gsub(/([a-z])([A-Z])/, '\1/\2')
sepp2k
2010-06-27 16:03:04
I prefer to use named character classes, since they will work with *all* upper/lower characters while explicitly listing them like you do will only work for, well, the ones between *a* and *z* : `"YingYang".gsub(/([[:lower:]])([[:upper:]])/, '\1/\2')`. Plus, it's more intention-revealing.
Jörg W Mittag
2010-06-27 19:31:51
@JörgWMittag: At least on 1.8.7 (with $KCODE set to "u") `"ÖÄÜ" =~ /[:upper:]/` returns nil, so I don't think it makes a difference.
sepp2k
2010-06-27 19:35:46
Yes, but it *does* work on Oniguruma, i.e. Ruby 1.9 and also 1.8 with the Oniguruma extension. It also works on JRuby both on 1.9 and 1.8, because it uses JOni (line-by-line Java transliteration of Oniguruma) even in 1.8 mode. And it doesn't hurt on MRI 1.8. Plus, it makes the intent clearer (IMO).
Jörg W Mittag
2010-06-27 19:52:24
glenn jackman
2010-06-28 13:58:34