Include module Module ActiveSupport::CoreExtensions::Array::Grouping

Discussion in 'Ruby' started by RichardOnRails, Mar 15, 2011.

  1. RichardOnRails, Mar 15, 2011
    #1
    1. Advertising

  2. RichardOnRails

    7stud -- Guest

    require "<your path to
    here->/lib/active_support/core_ext/array/grouping"

    Then you will have something like this at the top of your file:

    module A
    module B
    module C

    def in_group
    puts "in_group"
    end

    end
    end
    end


    Which means you can do this:

    class MyClass
    include A::B::C
    end

    MyClass.new.in_group

    --output:--
    in_group


    I think that Grouping is defined as a "mixin" module--in other words it
    is a module which contains regular def's inside it:

    module ActiveSupport
    module CoreExtension
    module Array
    module Grouping

    def in_group
    ..
    ..
    end

    end
    end
    end
    end


    You can't create a module instance, like you can with a class. So you
    are not able to create a Grouping instance:

    obj = Grouping.new

    and call the method using the instance:

    obj.in_group

    Rather mixin modules are created to be included by a class, and then the
    defs in the module become defs in the class.

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Mar 16, 2011
    #2
    1. Advertising

  3. RichardOnRails

    7stud -- Guest

    Or, you can also do something like this:

    module A
    module B
    module C

    def in_group
    puts 'in_group'
    end

    end
    end
    end


    include A::B::C
    in_group

    include() at the top-level causes the defs in the module to be included
    in the Object class, which means you can call in_group (without a
    receiver) from anywhere.

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Mar 16, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Bogenberger
    Replies:
    3
    Views:
    1,005
    Andreas Bogenberger
    Feb 22, 2008
  2. han
    Replies:
    0
    Views:
    223
  3. john peter
    Replies:
    3
    Views:
    306
    Logan Capaldo
    Mar 31, 2006
  4. Trans
    Replies:
    0
    Views:
    134
    Trans
    Sep 12, 2006
  5. Tim Bray
    Replies:
    0
    Views:
    106
    Tim Bray
    Oct 13, 2006
Loading...

Share This Page