Sha256: 4ea0c214078eb8d7171e9b3a8cceaf15691fe7599d3650bb73456783c8512399

Contents?: true

Size: 732 Bytes

Versions: 2

Compression:

Stored size: 732 Bytes

Contents

#          Copyright (c) 2006 Michael Fellinger m.fellinger@gmail.com
# All files in this distribution are subject to the terms of the Ruby license.

class Array
  def put_within(object, constrain)
    pre, post = constrain.values_at(:after, :before)

    unless rindex(post) - index(pre) == 1
      raise ArgumentError, "Too many elements within constrain"
    end

    put_after(pre, object)
  end

  def put_after(element, object)
    raise ArgumentError, "The given element doesn't exist" unless include?(element)
    self[index(element) + 1, 0] = object
  end

  def put_before(element, object)
    raise ArgumentError, "The given element doesn't exist" unless include?(element)
    self[rindex(element), 0] = object
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ramaze-0.2.0 lib/ramaze/snippets/array/put_within.rb
ramaze-0.2.1 lib/ramaze/snippets/array/put_within.rb