Module | Indexable |
In: |
lib/core/facets/indexable.rb
|
Indexable is a mixin that provides index based methods, working soley with four methods: index, slice, splice and size.
These methods work in harmony. Where index returns a position of a given element, slice returns elements for given positions. splice is like slice but replaces the given position with new values. This mehtod is not part of ruby core, but it generally just an alias for #[]=, just as slice is an alias of #[]. size of course simply returns the total length of the indexable object.
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).
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