Skip to content

Commit ddbdd07

Browse files
committed
Merge pull request #89 from bquorning/fix-sub-vs-chomp
sub-vs-chomp must use \z instead of $
2 parents 13717be + aff7aba commit ddbdd07

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -969,17 +969,17 @@ Note that this can only be used for removing characters from the end of a string
969969

970970
```
971971
$ ruby -v code/string/sub-vs-chomp.rb
972-
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin13]
972+
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin13]
973973
Calculating -------------------------------------
974-
String#sub/regexp/ 38.068k i/100ms
975-
String#chomp'string' 78.284k i/100ms
974+
String#sub/regexp/ 42.816k i/100ms
975+
String#chomp'string' 94.851k i/100ms
976976
-------------------------------------------------
977-
String#sub/regexp/ 560.625k17.1%) i/s - 2.703M
978-
String#chomp'string' 2.704M18.6%) i/s - 12.839M
977+
String#sub/regexp/ 660.509k 8.0%) i/s - 3.297M
978+
String#chomp'string' 2.803M 8.0%) i/s - 13.943M
979979
980980
Comparison:
981-
String#chomp'string': 2703881.6 i/s
982-
String#sub/regexp/: 560625.4 i/s - 4.82x slower
981+
String#chomp'string': 2803443.5 i/s
982+
String#sub/regexp/: 660508.7 i/s - 4.24x slower
983983
```
984984

985985
##### `attr_accessor` vs `getter and setter` [code](code/general/attr-accessor-vs-getter-and-setter.rb)

code/string/sub-vs-chomp.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
SLUG = 'YourSubclassType'
44

55
def slow
6-
SLUG.sub(/Type$/, '')
6+
SLUG.sub(/Type\z/, '')
77
end
88

99
def fast

0 commit comments

Comments
 (0)