This project is archived and is in readonly mode.

#5630 ✓stale
jack dempsey (jackdempsey)

active_support/inflector/transliterate using I18n without requiring it

Reported by jack dempsey (jackdempsey) | September 13th, 2010 @ 11:32 PM

I noticed this while trying to include active_support/core_ext/string/inflections and use camelize and parameterize. The first worked fine, while the second threw an error.

parameterize depends on transliterate, which is defined in transliterate.rb:

def transliterate(string, replacement = "?")
    ActiveSupport::Multibyte::Unicode.tidy_bytes(string), :c),
      :replacement => replacement)

That I18n exists without a require statement and hence trying to require these files and use on their own fails with the obvious error:

~ > ./foo.rb
/Users/jack/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/inflector/transliterate.rb:61:in transliterate': uninitialized constant ActiveSupport::Inflector::I18n (NameError)

from /Users/jack/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/inflector/transliterate.rb:83:in `parameterize'

If you add this to the top of the file:

require 'active_support/i18n'

then I18n will be defined, but will fail when it tries to load its hooks:

~ > ./foo.rb /Users/jack/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/i18n.rb:9:in <top (required)>': undefined methodrun_load_hooks' for ActiveSupport:Module (NoMethodError)

from <internal:lib/rubygems/custom_require>:29:in `require'

If we add a require for hooks into I18n like so:

require 'active_support/lazy_load_hooks'

then the script works as we'd expect:

!/usr/bin/env ruby

require 'active_support/inflector/transliterate'
include ActiveSupport::Inflector
p parameterize("foo bar")

~ > ./foo.rb "foo-bar"

So in summary,

  1. active_support/i18n.rb should require lazy_load_hooks if it's going to try and use them
  2. active_support/inflector/transliterate.rb should require active_support/i18n.rb if it wants to use it

I'm not sure how we should write a test for this. The inflector_test.rb in active_support does this:

require 'active_support'

which should probably be fixed as well. Thoughts?

Comments and changes to this ticket

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

Shared Ticket Bins

People watching this ticket