Class: LX::Array
- Inherits:
-
Object
- Object
- LX::Array
- Defined in:
- lib/lx.rb
Overview
A helper class to add methods to the Array class. When you use the Array::lx you’re creating an instance of LX::Array.
Instance Method Summary collapse
-
#deep_dup ⇒ Object
deep_dup.
-
#draw ⇒ Object
draw.
-
#initialize(p_arr) ⇒ Array
constructor
initialize a LX::Array object.
-
#move(old_idx, new_idx) ⇒ Object
Moves an array element from the old index to the new index.
-
#split(exp) ⇒ Object
Splits an array into an array of arrays.
Constructor Details
#initialize(p_arr) ⇒ Array
initialize a LX::Array object. The only param is the array itself.
196 197 198 |
# File 'lib/lx.rb', line 196 def initialize(p_arr) @arr = p_arr end |
Instance Method Details
#deep_dup ⇒ Object
deep_dup
245 246 247 |
# File 'lib/lx.rb', line 245 def deep_dup return LX.deep_dup(@arr) end |
#draw ⇒ Object
draw
238 239 240 241 242 |
# File 'lib/lx.rb', line 238 def draw rv = @arr[0] @arr.rotate! return rv end |
#move(old_idx, new_idx) ⇒ Object
Moves an array element from the old index to the new index.
arr = ['a', 'b', 'c']
arr.lx.move 1, 2 # => ["a", "c", "b"]
203 204 205 |
# File 'lib/lx.rb', line 203 def move(old_idx, new_idx) @arr.insert new_idx, @arr.slice!(1) end |
#split(exp) ⇒ Object
Splits an array into an array of arrays. The param is the delimiter.
arr = ['Fred', 'George', ':', 'Mary', 'Frank']
arr.lx.split ':' # => [["Fred", "George"], ["Mary", "Frank"]]
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/lx.rb', line 210 def split(exp) # $tm.hrm rv = [] current = nil # loop through array looking for delimiter @arr.each do |el| if el == exp if current rv.push current current = nil end else current ||= [] current.push el end end # add last current if current rv.push current end # return return rv end |