Class: Array
Overview
Copyright © 2008 Michael Fellinger [email protected] All files in this distribution are subject to the terms of the Ruby license.
Instance Method Summary collapse
- #put_after(element, object) ⇒ Object
- #put_before(element, object) ⇒ Object
- #put_within(object, constrain) ⇒ Object
Instance Method Details
#put_after(element, object) ⇒ Object
15 16 17 18 |
# File 'lib/ramaze/snippets/array/put_within.rb', line 15 def put_after(element, object) raise ArgumentError, "The given element doesn't exist" unless include?(element) self[index(element) + 1, 0] = object end |
#put_before(element, object) ⇒ Object
20 21 22 23 |
# File 'lib/ramaze/snippets/array/put_within.rb', line 20 def put_before(element, object) raise ArgumentError, "The given element doesn't exist" unless include?(element) self[rindex(element), 0] = object end |
#put_within(object, constrain) ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/ramaze/snippets/array/put_within.rb', line 5 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 |