Module: Enumerable
- Included in:
- SwissMatch::Cantons, SwissMatch::Communities, SwissMatch::ZipCodes
- Defined in:
- lib/swissmatch/ruby.rb
Overview
This file provides a couple of ruby monkey patches.
Instance Method Summary (collapse)
-
- (Object) last(n = nil)
The last element(s) of self.
Instance Method Details
- (Object) last(n = nil)
The last element(s) of self. If the enumerable is empty, the first form returns nil, the second an empty Array. The method is optimized to make use of reverse_each if present.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/swissmatch/ruby.rb', line 15 def last(n=nil) reverse_each_method = method(:reverse_each) has_reverse_each = reverse_each_method && reverse_each_method.owner != Enumerable # native reverse_each needed if n then return_value = [] if has_reverse_each then reverse_each { |val| return_value.unshift(val) return return_value if return_value.size == n } else each { |val| return_value.push(val) return_value.shift if return_value.size > n } end else if has_reverse_each then reverse_each { |value| return value } else return_value = nil each { |value| return_value = value } end end return_value end |