I have a string that can either be something like
create
or
create by
depending on the verb, etc. In Ruby (on Rails) to get the past tense
string.sub(/e?$/, "ed")
or
string.sub(/ by?$/, "ed by")
works, but is there any way to combine the two? With some type of conditional statement or similar.
Using word boundary (\b
):
'create by'.sub(/e\b/, 'ed')
# => "created by"
'create'.sub(/e\b/, 'ed')
# => "created"
Why not?
2.1.0-preview2 :046 > 'create'.sub('create', 'created')
=> "created"
2.1.0-preview2 :047 > 'create by'.sub('create', 'created')
=> "created by"
And no regexps... )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With