Indexable

Indexable

Indexable is a mixin that provides index based methods, working soley with four methods: #, #, # and #.

These methods work in harmony. Where # returns a position of a given element, # returns elements for given positions. # is like # 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 # is an alias of #[]. # of course simply returns the total length of the indexable object.

Public Instance Methods

body() click to toggle source

Returns an array of the first element upto, but not including, the last element.

  [1,2,3].body  #=> [1,2]
    # File lib/core/facets/indexable.rb, line 71
71:   def body
72:     slice(0,size-1)
73:   end
ends() click to toggle source

A shorting of “ends at”, returns the last index of the indexable object. Returns nil if there are no elements.

  [1,2,3,4,5].ends  #=> 4

This nearly equivalent to +size - 1+.

     # File lib/core/facets/indexable.rb, line 192
192:   def ends
193:     return nil if size == 0
194:     size - 1
195:   end
first(n=1) click to toggle source

Returns first n elements.

  "Hello World".first(3)  #=> "Hel"
     # File lib/core/facets/indexable.rb, line 130
130:   def first(n=1)
131:     slice(0, n.to_i)
132:   end
first!() click to toggle source

Remove and return the first element.

  a = [1,2,3]
  a.first!      #=> 1
  a             #=> [2,3]
     # File lib/core/facets/indexable.rb, line 170
170:   def first!
171:     splice(0)
172:   end
first=(x) click to toggle source

Change the first element.

  a = ["a","y","z"]
  a.first = "x"
  p a           #=> ["x","y","z"]
     # File lib/core/facets/indexable.rb, line 150
150:   def first=(x)
151:     splice(0,x)
152:   end
foot() click to toggle source

Like #, returning the last element in an array.

  [1,2,3].foot  #=> [3]
    # File lib/core/facets/indexable.rb, line 58
58:   def foot
59:     slice(1,1)
60:   end
head() click to toggle source

Like # but returns the first element in a new array.

  [1,2,3].head  #=> [1]
    # File lib/core/facets/indexable.rb, line 41
41:   def head
42:     slice(0,1)
43:   end
index(obj=nil, &block) click to toggle source

Returns the index of the first element equal to the given object or satisfying the block condition.

  [1,2,3,4].index{ |e| e == 3 }  #=> 2
  [1,2,3,4].index{ |e| e > 3 }   #=> 3
     # File lib/core/facets/indexable.rb, line 217
217:   def index(obj=nil, &block)
218:     if block_given?
219:       size.times do |i|
220:         return i if yield(slice(i))
221:       end
222:     else
223:       size.times do |i|
224:         return i if obj == slice(i)
225:       end
226:     end
227:     nil
228:   end
Also aliased as: index_of
index_of(obj=nil, &block) click to toggle source

to be deprecated

Alias for: index
last(n=1) click to toggle source

Returns last n elements.

  "Hello World".last(3)  #=> "rld"
     # File lib/core/facets/indexable.rb, line 138
138:   def last(n=1)
139:     n = n.to_i
140:     return self if n > size
141:     slice(-n, n) #slice(-n..-1)
142:   end
last!() click to toggle source

Remove and return the last element.

  a = [1,2,3]
  a.last!       #=> 3
  a             #=> [1,2]
     # File lib/core/facets/indexable.rb, line 180
180:   def last!
181:     splice(1)
182:   end
last=(x) click to toggle source

Change the last element.

  a = [1,2,5]
  a.last = 3
  p a           #=> [1,2,3]
     # File lib/core/facets/indexable.rb, line 160
160:   def last=(x)
161:     splice(1,x)
162:   end
mid(offset=0) click to toggle source

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).

    # File lib/core/facets/indexable.rb, line 89
89:   def mid(offset=0)
90:     slice((size / 2) + offset)
91:   end
middle() click to toggle source

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].middle        #=> 3
  [1,2,3,4,5,6].middle      #=> [3,4]

In contrast to # which utilizes an offset.

     # File lib/core/facets/indexable.rb, line 102
102:   def middle
103:     if size % 2 == 0
104:      slice( (size / 2) - 1, 2 )
105:      #[slice((size / 2) - 1, 1), slice(size / 2, 1)]
106:     else
107:       slice(size / 2)
108:     end
109:   end
pos(i) click to toggle source

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
     # File lib/core/facets/indexable.rb, line 203
203:   def pos(i)
204:     if i > 0
205:       return i - 1
206:     else
207:       size + i
208:     end
209:   end
range(a=nil,z=nil) click to toggle source

Returns the index range between two elements. If no elements are given, returns the range from first to last.

  ['a','b','c','d'].range            #=> 0..3
  ['a','b','c','d'].range('b','d')   #=> 1..2
     # File lib/core/facets/indexable.rb, line 242
242:   def range(a=nil,z=nil)
243:     if !a
244:       0..(size-1)
245:     else
246:       index(a)..index(z)
247:     end
248:   end
tail() click to toggle source

Returns an array from second element to last element.

  [1,2,3].tail  #=> [2,3]
    # File lib/core/facets/indexable.rb, line 49
49:   def tail
50:     slice(1,length-1)
51:   end
thru(from, to) click to toggle source

Fetch values from a start index thru an end index.

  [1,2,3,4,5].thru(0,2)  #=> [1,2,3]
  [1,2,3,4,5].thru(2,4)  #=> [3,4,5]
     # File lib/core/facets/indexable.rb, line 116
116:   def thru(from, to)
117:     a = []
118:     i = from
119:     while i <= to
120:       a << slice(i)
121:       i += 1
122:     end
123:     a
124:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.