From 792f625bec5b6c1ab2629e0a6c353df8cc70920e Mon Sep 17 00:00:00 2001 From: Brian McManus Date: Fri, 20 Mar 2015 10:28:25 -0700 Subject: Fixes Inflector#titleize to work with SafeBuffer The way Inflector#titleize was implemented did not work properly when called on a SafeBuffer object. Using the global `$&` variable in the gsub resulted in calling capitalize on a nil object for reasons I still do not fully understand. Removing the UNSAFE_STRING_METHODS override for the gsub method in SafeBuffer "fixed" the bug but is obviously unacceptable. An example of this is very easy to see in rails console: ActiveSupport::SafeBuffer.new("my test").titleize > NoMethodError: undefined method `capitalize' for nil:NilClass Using the non global version of gsub with a |match| arg passed to the block fixes the problem. Again I do not quite understand why. I noticed that other parts of Inflector were already using the standard block arg version of gsub so I don't think it should be a problem to convert this method to using it as well. --- activesupport/lib/active_support/inflector/methods.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/inflector/methods.rb b/activesupport/lib/active_support/inflector/methods.rb index fe8a2ac9ba..9d27cb7073 100644 --- a/activesupport/lib/active_support/inflector/methods.rb +++ b/activesupport/lib/active_support/inflector/methods.rb @@ -153,7 +153,9 @@ module ActiveSupport # titleize('TheManWithoutAPast') # => "The Man Without A Past" # titleize('raiders_of_the_lost_ark') # => "Raiders Of The Lost Ark" def titleize(word) - humanize(underscore(word)).gsub(/\b(?