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
a = [1, 2, 3] a.put_after(2, 4) a # => [1, 2, 4, 3].
-
#put_before(element, object) ⇒ Object
a = [1, 2, 3] a.put_before(2, 4) a # => [1, 4, 2, 3].
-
#put_within(object, constrain) ⇒ Object
a = [1, 2, 3] a.put_within(4, :after => 2, :before => 3) a # => [1, 2, 4, 3].
Instance Method Details
#put_after(element, object) ⇒ Object
a = [1, 2, 3]
a.put_after(2, 4)
a # => [1, 2, 4, 3]
24 25 26 27 |
# File 'lib/ramaze/snippets/array/put_within.rb', line 24 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
a = [1, 2, 3]
a.put_before(2, 4)
a # => [1, 4, 2, 3]
33 34 35 36 |
# File 'lib/ramaze/snippets/array/put_within.rb', line 33 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
a = [1, 2, 3]
a.put_within(4, :after => 2, :before => 3)
a # => [1, 2, 4, 3]
10 11 12 13 14 15 16 17 18 |
# File 'lib/ramaze/snippets/array/put_within.rb', line 10 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 |