reorder 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/reorder.rb +100 -0
- metadata +46 -0
data/lib/reorder.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
class Array
|
2
|
+
|
3
|
+
def reorder(from, to)
|
4
|
+
if ((from < 0 - self.length or from > self.length - 1) and (to < 0 - self.length or to > self.length - 1))
|
5
|
+
# 'from' and 'to' indexes are out of range (indexes can be negative)
|
6
|
+
raise "From and to indexes are out of range."
|
7
|
+
elsif from < 0 - self.length or from > self.length - 1
|
8
|
+
# 'from' index is out of range (indexes can be negative)
|
9
|
+
raise "From index is out of range."
|
10
|
+
elsif to < 0 - self.length or to > self.length - 1
|
11
|
+
# 'to' index is out of range (indexes can be negative)
|
12
|
+
raise "To index is out of range."
|
13
|
+
elsif to == from
|
14
|
+
# 'from' and 'to' indexes are the same so do nothing, return self
|
15
|
+
self
|
16
|
+
else
|
17
|
+
# copy and return reordered array
|
18
|
+
self.clone.reorder!(from, to)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def reorder!(from, to)
|
23
|
+
# flag for debug output
|
24
|
+
debug = false
|
25
|
+
|
26
|
+
# if debug show arguments passed to function
|
27
|
+
puts "reorder(#{from}, #{to})" if debug
|
28
|
+
|
29
|
+
if ((from < 0 - self.length or from > self.length - 1) and (to < 0 - self.length or to > self.length - 1))
|
30
|
+
# 'from' and 'to' indexes are out of range (indexes can be negative)
|
31
|
+
raise "From and to indexes are out of range."
|
32
|
+
elsif from < 0 - self.length or from > self.length - 1
|
33
|
+
# 'from' index is out of range (indexes can be negative)
|
34
|
+
raise "From index is out of range."
|
35
|
+
elsif to < 0 - self.length or to > self.length - 1
|
36
|
+
# 'to' index is out of range (indexes can be negative)
|
37
|
+
raise "To index is out of range."
|
38
|
+
elsif to == from
|
39
|
+
# 'from' and 'to' indexes are the same so do nothing, return nil
|
40
|
+
nil
|
41
|
+
else
|
42
|
+
# reorder array
|
43
|
+
|
44
|
+
# save 'to' element for last iteration of loop below
|
45
|
+
temp = self[to]
|
46
|
+
# if debug show statement saving 'to' element
|
47
|
+
puts "temp = self[#{to}]" if debug
|
48
|
+
|
49
|
+
if from < to
|
50
|
+
# 'from' index is less than 'to' index so loop over increasing values
|
51
|
+
|
52
|
+
# loop through increasing array indexes
|
53
|
+
(from..to).each_with_index do |index|
|
54
|
+
if index == from
|
55
|
+
# first iteration move 'from' element to 'to' element
|
56
|
+
self[to] = self[from]
|
57
|
+
# if debug show statement moving 'from' element to 'to' element
|
58
|
+
puts "first iteration self[#{to}] = self[#{from}]" if debug
|
59
|
+
elsif index == to
|
60
|
+
# last iteration move saved 'to' element next to where it originated
|
61
|
+
self[index - 1] = temp
|
62
|
+
# if debug show statement moving saved 'to' element
|
63
|
+
puts "last iteration self[#{index - 1}] = temp" if debug
|
64
|
+
else
|
65
|
+
# intermediate iterations move index element one element to the left
|
66
|
+
self[index - 1] = self[index]
|
67
|
+
# if debug show statement moving index element one element to the left
|
68
|
+
puts "intermediate iteration self[#{index - 1}] = self[#{index}]" if debug
|
69
|
+
end
|
70
|
+
end
|
71
|
+
else
|
72
|
+
# 'to' index is less than 'from' index so loop over decreasing values
|
73
|
+
|
74
|
+
# loop through decreasing array indexes
|
75
|
+
index = from
|
76
|
+
from.downto(to) do
|
77
|
+
if index == from
|
78
|
+
# first iteration move 'from' element to 'to' element
|
79
|
+
self[to] = self[from]
|
80
|
+
# if debug show statement moving 'from' element to 'to' element
|
81
|
+
puts "first self[#{to}] = self[#{from}]" if debug
|
82
|
+
elsif index == to
|
83
|
+
# last iteration move saved 'to' element next to where it originated
|
84
|
+
self[index + 1] = temp
|
85
|
+
# if debug show statement moving saved 'to' element
|
86
|
+
puts "last self[#{index + 1}] = temp" if debug
|
87
|
+
else
|
88
|
+
# intermediate iterations move index element one element to the right
|
89
|
+
self[index + 1] = self[index]
|
90
|
+
# if debug show statement moving index element one element to the right
|
91
|
+
puts "intermediate self[#{index + 1}] = self[#{index}]" if debug
|
92
|
+
end
|
93
|
+
index -= 1
|
94
|
+
end
|
95
|
+
end
|
96
|
+
# return reordered array
|
97
|
+
self
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: reorder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chris Tsongas
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-21 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Reorder the elements in an array by moving an element from one index
|
15
|
+
to another and adjusting the other elements accordingly.
|
16
|
+
email: chris.tsongas@bitmojo.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/reorder.rb
|
22
|
+
homepage: http://rubygems.org/gems/reorder
|
23
|
+
licenses: []
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 1.8.24
|
43
|
+
signing_key:
|
44
|
+
specification_version: 3
|
45
|
+
summary: Reorder Array
|
46
|
+
test_files: []
|