Sha256: 416c0f1c098bfbfbf4de4816277dd31ca8259a9cabafe98349c9cfa0f2eff60c
Contents?: true
Size: 765 Bytes
Versions: 26
Compression:
Stored size: 765 Bytes
Contents
class Array # Returns the middle element(s) of an array. Even-sized arrays, # not having an exact middle, returns a two-element array # of the two middle elements. # # [1,2,3,4,5].mid #=> 3 # [1,2,3,4,5,6].mid #=> [3,4] # # In contrast to #mid which utilizes an offset. def middle if size % 2 == 0 [ at((size/2)-1), at(size/2) ] else at(size/2) end end end # _____ _ # |_ _|__ ___| |_ # | |/ _ \/ __| __| # | | __/\__ \ |_ # |_|\___||___/\__| # =begin test require 'test/unit' class TCArray < Test::Unit::TestCase def test_middle a = [1,2,3,4,5] b = [1,2,3,4,5,6] assert_equal( 3, a.middle ) assert_equal( [3,4], b.middle ) end end =end
Version data entries
26 entries across 26 versions & 1 rubygems