instance method
parameterize
Ruby on Rails edge
Signature
parameterize(string, separator: "-", preserve_case: false, locale: nil)
Replaces special characters in a string so that it may be used as part of a ‘pretty’ URL.
“‘ parameterize(“Donald E. Knuth”) # => “donald-e-knuth” parameterize(“^très|Jolie– ”) # => “tres-jolie” “`
To use a custom separator, override the separator argument.
“‘ parameterize(“Donald E. Knuth”, separator: ’_’) # => “donald_e_knuth” parameterize(“^très|Jolie__ ”, separator: ‘_’) # => “tres_jolie” “‘
To remove the unwanted characters instead of replacing them, pass an empty separator. nil is treated the same way.
“‘ parameterize(“Donald E. Knuth”, separator: ”) # => “donaldeknuth” “`
To preserve the case of the characters in a string, use the preserve_case argument.
“‘ parameterize(“Donald E. Knuth”, preserve_case: true) # => “Donald-E-Knuth” parameterize(“^très|Jolie– ”, preserve_case: true) # => “tres-Jolie” “`
It preserves dashes and underscores unless they are used as separators:
“‘ parameterize(“^très|Jolie__ ”) # => “tres-jolie__” parameterize(“^très|Jolie– ”, separator: “_”) # => “tres_jolie–” parameterize(“^très_Jolie– ”, separator: “.”) # => “tres_jolie–” “`
If the optional parameter locale is specified, the word will be parameterized as a word of that language. By default, this parameter is set to nil and it will use the configured I18n.locale.
Parameters
-
stringreq -
separatorkey = "-" -
preserve_casekey = false -
localekey = nil
Source
# File activesupport/lib/active_support/inflector/transliterate.rb, line 149
def parameterize(string, separator: "-", preserve_case: false, locale: nil)
separator ||= ""
# Replace accented chars with their ASCII equivalents.
parameterized_string = transliterate(string, locale: locale)
# Turn unwanted chars into the separator. tr_s! does it in place, squeezing each
# run into one separator, without a MatchData and a replacement String per run;
# match? spares it building its table when there is nothing to replace. tr_s!
# leaves chars above U+00FF untouched, which a custom transliteration rule can
# return, hence the regexp for those strings.
if separator.length == 1 && separator.ascii_only? && parameterized_string.ascii_only?
parameterized_string.tr_s!("^a-zA-Z0-9\\-_", separator) unless parameterized_string.match?(/\A[a-z0-9\-_]*\z/i)
else
parameterized_string.gsub!(/[^a-z0-9\-_]+/i, separator)
end
unless separator.empty?
# No more than one of the separator in a row.
if separator.length == 1
parameterized_string.squeeze!(separator)
else
re_sep = Regexp.escape(separator)
parameterized_string.gsub!(/#{re_sep}{2,}/, separator)
end
# Remove leading/trailing separator.
parameterized_string.delete_prefix!(separator)
parameterized_string.delete_suffix!(separator)
end
parameterized_string.downcase! unless preserve_case
parameterized_string
end
Defined in activesupport/lib/active_support/inflector/transliterate.rb line 149
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveSupport::Inflector