Richard3
(Richard)
15 March 2011 06:48
1
I want to use the grouping method(s) from the subject module, but I
can't translate it address into a ruby include.
The module's documented at http://rails.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Array/Grouping.html .
Thanks in Advance for any guidance offered.
Richard
7stud
(7stud --)
16 March 2011 01:03
2
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: :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
(7stud --)
16 March 2011 02:02
3
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: :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/ .