Sha256: 126bb7671f0ccd855709dbb5d602805dfdce2987854b2159725dded8e4ad25fd
Contents?: true
Size: 1.05 KB
Versions: 26
Compression:
Stored size: 1.05 KB
Contents
class Array # Returns the middle element of an array, or the element offset # from middle if the parameter is given. Even-sized arrays, # not having an exact middle, return the middle-right element. # # [1,2,3,4,5].mid #=> 3 # [1,2,3,4,5,6].mid #=> 4 # [1,2,3,4,5,6].mid(-1) #=> 3 # [1,2,3,4,5,6].mid(-2) #=> 2 # [1,2,3,4,5,6].mid(1) #=> 5 # # In other words, If there are an even number of elements the # higher-indexed of the two center elements is indexed as # orgin (0). def mid(offset=0) self.at( (self.length / 2) + offset ) end end # _____ _ # |_ _|__ ___| |_ # | |/ _ \/ __| __| # | | __/\__ \ |_ # |_|\___||___/\__| # =begin test require 'test/unit' class TCArray < Test::Unit::TestCase def test_mid a = [1,2,3,4,5] b = [1,2,3,4,5,6] assert_equal( 3, a.mid ) assert_equal( 4, b.mid ) assert_equal( 4, a.mid(1) ) assert_equal( 5, b.mid(1) ) assert_equal( 6, b.mid(2) ) assert_equal( 3, b.mid(-1) ) end end =end
Version data entries
26 entries across 26 versions & 1 rubygems