class Array
Public Instance Methods
Calculate the longest common prefix for all elements in the array.
Requires array elements to support size and take operations
@api public @return [Object] The largest common prefix for all elements in the array @example [['a', 'b'], ['a', 'b', 'c', 'd'], ['a', 'b', 'e', 'f']].prefix # => ['a', 'b']
# File lib/snmputils/arrayutils.rb, line 22 def prefix pairwise_prefix = lambda do |short, long| if short.size == 0 or long .size == 0 short.class.new else if short.size > long.size pairwise_prefix.call(long, short) elsif short == long.take(short.size) short else pairwise_prefix.call(short.take(short.size - 1), long) end end end reduce &pairwise_prefix end
Convenience method to return the second element in the list
@api public @return [Object] The second object in the array @example ['a', 'b', 'c'].second # => 'b'
# File lib/snmputils/arrayutils.rb, line 11 def second return at(1) end
Calculate the longest common suffix for all elements in the array.
Requires array elements to support size and take operations
@api public @return [Object] The largest common suffix for all elements in the array @example [['y', 'z'], ['w', 'x', 'y', 'z'], ['u', 'v', 'y', 'z']].suffix # => ['y', 'z']
# File lib/snmputils/arrayutils.rb, line 46 def suffix prefix = (map { |x| x != nil && x.reverse }).prefix prefix.reverse if prefix end