lib/facet/array/pos.rb in facets-0.9.0 vs lib/facet/array/pos.rb in facets-1.0.0

- old
+ new

@@ -1,38 +1,76 @@ -require 'nano/array/pos.rb' \ No newline at end of file +class Array + # Returns the positive ordinal index given + # a cardinal position, 1 to n or -n to -1. + # + # [1,2,3,4,5].pos(1) #=> 0 + # [1,2,3,4,5].pos(-1) #=> 4 + # + def pos(i) + if i > 0 + return i - 1 + else + self.length + i + end + end +end + + + +# _____ _ +# |_ _|__ ___| |_ +# | |/ _ \/ __| __| +# | | __/\__ \ |_ +# |_|\___||___/\__| +# +=begin test + + require 'test/unit' + + class TCArray < Test::Unit::TestCase + + def test_pos + a = [1,2,3,4,5] + assert_equal( 0, a.pos(1) ) + assert_equal( 4, a.pos(-1) ) + end + + end + +=end