Are there any performance implications of the .gsub chain and / or .sub methods for a string in Ruby?
For example, here is an example of a method from a Rails source that creates an alt tag for images. It removes the file extension and digest (if any).
def image_alt(src) File.basename(src, '.*').sub(/-[[:xdigit:]]{32}\z/, '').capitalize end
In my application, I want it to change underscores or hyphens to space, so I want to add the gsub method at the end:
def image_alt(src) File.basename(src, '.*').sub(/-[[:xdigit:]]{32}\z/, '').gsub(/(_|-)/, ' ').capitalize end
Does this raise red flags in terms of performance or style?
source share