Fix `String#parameterize` raising `TypeError` when separator is nil by hammadxcm · Pull Request #58324 · rails/rails · GitHub
Skip to content

Fix String#parameterize raising TypeError when separator is nil - #58324

Merged
byroot merged 1 commit into
rails:mainfrom
hammadxcm:fix-parameterize-nil-separator
Aug 1, 2026
Merged

byroot merged 1 commit into
rails:mainfrom
hammadxcm:fix-parameterize-nil-separator

Conversation

@hammadxcm

Copy link
Copy Markdown
Contributor

Summary

ActiveSupport::Inflector#parameterize contains a guard that explicitly handles a nil separator:

unless separator.nil? || separator.empty?
  # No more than one of the separator in a row.
  # Remove leading/trailing separator.
end

But three lines earlier the separator is passed straight to String#gsub!:

parameterized_string.gsub!(/[^a-z0-9\-_]+/i, separator)

so a nil separator raises before the guard is ever reached, and the separator.nil? branch is unreachable:

"Donald E. Knuth".parameterize(separator: nil)
# => TypeError: no implicit conversion of nil into String

Fix

Treat a nil separator like an empty one — which is exactly what the existing guard already implies — and drop the now-redundant separator.nil? check:

"Donald E. Knuth".parameterize(separator: nil) # => "donaldeknuth"
"Donald E. Knuth".parameterize(separator: "")  # => "donaldeknuth"

The alternative reading — that nil was never meant to be supported — would mean deleting the separator.nil? branch instead. Making it work seemed like the smaller surprise for callers that pass a separator through from configuration or params, but happy to flip it to the removal if maintainers prefer that.

Both separator: nil and separator: "" now go down the same path, so no behavior changes for any currently-working input.

Tests

Added separator: nil cases mirroring the existing separator: "" tests, including the preserve_case: true variant, reusing the existing StringToParameterizeWithNoSeparator and StringToParameterizePreserveCaseWithNoSeparator fixtures.

activesupport — string_ext_test.rb, inflector_test.rb, transliterate_test.rb: 645 runs, 3207 assertions, 0 failures, 0 errors. RuboCop clean.

`parameterize` guards against a `nil` separator when squeezing duplicate
separators and trimming leading/trailing ones:

    unless separator.nil? || separator.empty?

But the `gsub!` a few lines above passes the separator straight to
`String#gsub!`, so a `nil` separator raised `TypeError` before that guard
was ever reached, making the `separator.nil?` branch unreachable.

Treat a `nil` separator like an empty one, which is what the existing
guard already implies:

    "Donald E. Knuth".parameterize(separator: nil) # => "donaldeknuth"
@byroot
byroot force-pushed the fix-parameterize-nil-separator branch from 4445604 to 6c6421a Compare August 1, 2026 08:14
@byroot
byroot merged commit 1925e83 into rails:main Aug 1, 2026
3 of 5 checks passed
byroot added a commit that referenced this pull request Aug 1, 2026
Fix `String#parameterize` raising `TypeError` when separator is nil
byroot added a commit that referenced this pull request Aug 1, 2026
Fix `String#parameterize` raising `TypeError` when separator is nil
@hammadxcm
hammadxcm deleted the fix-parameterize-nil-separator branch August 1, 2026 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants