From bb006f98c1a384b9b3913e3ad3d9b46dbdfe54e4 Mon Sep 17 00:00:00 2001 From: Reinier Balt Date: Wed, 19 Aug 2015 15:24:35 +0200 Subject: [PATCH] Using tr is faster than gsub Using tr is faster than gsub when replacing a single character in a string with another single character. Also freeze constant strings --- app/models/tag.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/models/tag.rb b/app/models/tag.rb index acbc3281..5c54575f 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -3,8 +3,8 @@ class Tag < ActiveRecord::Base has_many :taggings has_many :taggable, :through => :taggings - DELIMITER = "," # Controls how to split and join tagnames from strings. You may need to change the validates_format_of parameters if you change this. - JOIN_DELIMITER = ", " + DELIMITER = ",".freeze # Controls how to split and join tagnames from strings. You may need to change the validates_format_of parameters if you change this. + JOIN_DELIMITER = ", ".freeze # If database speed becomes an issue, you could remove these validations and # rescue the ActiveRecord database constraint errors instead. @@ -17,11 +17,11 @@ class Tag < ActiveRecord::Base # allow tags to be renamed later, you might want to use the # before_save callback instead. def before_create - self.name = name.downcase.strip.squeeze(" ") + self.name = name.downcase.strip.squeeze(' '.freeze) end def label - @label ||= name.gsub(' ', '-') + @label ||= name.tr(' '.freeze, '-'.freeze) end def to_s