Module: Enumerable
- Defined in:
- lib/sortsmith/core_ext/enumerable.rb
Overview
Extensions to Ruby's built-in Enumerable module.
Sortsmith extends Enumerable to provide enhanced sorting capabilities while preserving the original behavior when used with blocks.
The key enhancement is allowing sort_by to be called without a block,
which returns a Sortsmith::Sorter instance for method chaining.
Instance Method Summary collapse
-
#og_sort_by ⇒ Object
private
Stores the original sort_by method before extension.
-
#sort_by(field = nil, *positional, **keyword, &block) ⇒ Array, Sortsmith::Sorter
Enhanced sort_by that supports both traditional block usage and direct field extraction.
Instance Method Details
#og_sort_by ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Stores the original sort_by method before extension.
This alias preserves Ruby's original sort_by behavior, allowing
Sortsmith to enhance the method while maintaining full backward
compatibility when blocks are provided.
33 |
# File 'lib/sortsmith/core_ext/enumerable.rb', line 33 alias_method :og_sort_by, :sort_by |
#sort_by(field = nil, *positional, **keyword, &block) ⇒ Array, Sortsmith::Sorter
This method maintains full backward compatibility with Ruby's original sort_by
When field is nil, returns a plain Sorter instance for manual chaining
Enhanced sort_by that supports both traditional block usage and direct field extraction.
This method extends Ruby's built-in sort_by to provide a fluent, chainable API
for sorting operations. When called with a block, it behaves exactly like the
original sort_by. When called without a block, it returns a Sortsmith::Sorter
instance for method chaining.
The direct syntax (sort_by(field)) provides a concise way to sort by a specific
field or method without verbose chaining, making simple sorting operations more
readable and intuitive.
101 102 103 104 105 106 107 108 |
# File 'lib/sortsmith/core_ext/enumerable.rb', line 101 def sort_by(field = nil, *positional, **keyword, &block) return og_sort_by(&block) if block sorter = Sortsmith::Sorter.new(self) return sorter if field.nil? sorter.extract(field, *positional, **keyword) end |