mbigras mbigras - 5 days ago 6
Ruby Question

How does active_support/inflector get included?

I required

active_support/inflector
and now I'm able to use the
pluralize
method on strings.

require 'active_support/inflector'

module Foodie
class Food
def self.pluralize(word)
word.pluralize
end
end
end

p Foodie::Food.pluralize("foo") #=> foos


But I was expecting to have to include the
ActiveSupport::Inflector
module as a mixin for it to work.

Why don't I need something like:

module Foodie
class Food
include ActiveSupport::Inflector
def self.pluralize(word)
word.pluralize
end
end
end

p Foodie::Food.pluralize("foo") #=> foos


Also the above example still works.

I thought the whole point of requiring a file is just so you have access to the classes/modules/methods/variables inside that file, but it doesn't necessarily include that functionality in all the modules and classes.

Maybe, is it because the ruby file with the Inflector module is opening up the string class? Like in this example:

foo.rb:

module Foo
end

class String
def cats
self + " cats"
end
end


bar.rb:

require_relative 'foo'

p "foobar".cats #=> "foobar cats"


Would love some guidance :)

Answer
Comments