class Object

Public Instance Methods

bottom(number) click to toggle source
# File lib/sliced_bread.rb, line 10
def bottom(number)
  if number.to_i < 0 or size <= number.to_i
    self
  else
    self[size-number.to_i...size]
  end
end
delete_if_contains(substring) click to toggle source
# File lib/sliced_bread.rb, line 42
def delete_if_contains(substring)
  self.delete_if{|el| el.match(substring)}
end
middle(count) click to toggle source
# File lib/sliced_bread.rb, line 18
def middle(count)
  array_count = self.count
  selection_indices = Array.new

  if array_count.even?
    # even array
    selection_indices.push(array_count/2-1, array_count/2)
  else
    # odd array
    selection_indices.push(array_count/2)
  end
  count_adjust = count - selection_indices.count
  left_buffer = count_adjust % 2
  left_limit = selection_indices.first - left_buffer - count_adjust/2
  right_limit = selection_indices.last + count_adjust/2

  case count
  when count < selection_indices.count
    self[left_limit]
  else
    self[left_limit..right_limit]
  end
end
top(number) click to toggle source
# File lib/sliced_bread.rb, line 2
def top(number)
  if size <= number.to_i
    self
  else
    self[0...number.to_i]
  end
end