Class: SortedArray::DefaultSorter
- Inherits:
-
Object
- Object
- SortedArray::DefaultSorter
- Defined in:
- lib/sorted_array/default_sorter.rb
Overview
Sorts by comparing of :field a <=> b.
Descend your Sorter from this class. It must at least define a method 'sort'.
Instance Attribute Summary collapse
-
#method ⇒ Object
readonly
Returns the value of attribute method.
Instance Method Summary collapse
-
#initialize(_method) ⇒ DefaultSorter
constructor
A new instance of DefaultSorter.
-
#marshal_dump ⇒ Array
- Classname, methodname.
- #marshal_load(array) ⇒ Object
-
#sort(object) ⇒ Object
abstract
Sort the given object.
Constructor Details
#initialize(_method) ⇒ DefaultSorter
Returns a new instance of DefaultSorter.
25 26 27 |
# File 'lib/sorted_array/default_sorter.rb', line 25 def initialize(_method) @method = _method end |
Instance Attribute Details
#method ⇒ Object (readonly)
Returns the value of attribute method.
22 23 24 |
# File 'lib/sorted_array/default_sorter.rb', line 22 def method @method end |
Instance Method Details
#marshal_dump ⇒ Array
Returns - Classname, methodname.
39 40 41 |
# File 'lib/sorted_array/default_sorter.rb', line 39 def marshal_dump [ self.class, @method ] end |
#marshal_load(array) ⇒ Object
44 45 46 |
# File 'lib/sorted_array/default_sorter.rb', line 44 def marshal_load array @method = array.last end |
#sort(object) ⇒ Object
This method is abstract.
Overwrite this method in descendants
Sort the given object. to sort your objects for your needs
34 35 36 |
# File 'lib/sorted_array/default_sorter.rb', line 34 def sort(object) object.sort! { |a,b| a.send(@method) <=> b.send(@method) } end |