arraybooster 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/arraybooster.rb +58 -0
- metadata +68 -0
data/lib/arraybooster.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
class Array
|
2
|
+
|
3
|
+
def anypop list, numbers = nil
|
4
|
+
if numbers.nil?
|
5
|
+
popped = list.pop
|
6
|
+
return popped, list
|
7
|
+
else
|
8
|
+
popped = []
|
9
|
+
numbers.times do |i|
|
10
|
+
popped << list.pop
|
11
|
+
end
|
12
|
+
return popped, list
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def lpush element
|
17
|
+
self.insert(0, element)
|
18
|
+
end
|
19
|
+
|
20
|
+
def rpush element
|
21
|
+
self.insert(-1, element)
|
22
|
+
end
|
23
|
+
|
24
|
+
def ppush element, priority
|
25
|
+
if priority - 1 > self.length
|
26
|
+
self.insert(-1, element)
|
27
|
+
elsif priority - 1 < 0
|
28
|
+
self.insert(0, element)
|
29
|
+
else
|
30
|
+
self.insert(priority - 1, element)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def lpop numbers = nil
|
35
|
+
return nil if self.length == 0
|
36
|
+
reversed_self = self.reverse
|
37
|
+
popped, list = anypop(reversed_self, numbers)
|
38
|
+
self.replace(list.reverse)
|
39
|
+
return popped
|
40
|
+
end
|
41
|
+
|
42
|
+
def rpop numbers = nil
|
43
|
+
return nil if self.length == 0
|
44
|
+
popped, list = anypop(self, numbers)
|
45
|
+
self.replace(list)
|
46
|
+
return popped
|
47
|
+
end
|
48
|
+
|
49
|
+
def substitute element, substitute_element
|
50
|
+
self.count(element).times do |i|
|
51
|
+
index = self.index(element)
|
52
|
+
self.delete_at(index)
|
53
|
+
self.insert(index, substitute_element)
|
54
|
+
end
|
55
|
+
self
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: arraybooster
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Edwin Rozario
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-04-24 00:00:00 +05:30
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Features such as Substitute, Rpush, Rpop, Lpush, Lpop, PriorityPush added to Array
|
23
|
+
email:
|
24
|
+
- rozarioed@gmail.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- lib/arraybooster.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: https://github.com/EdwinRozario/ArrayBooster
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
hash: 3
|
48
|
+
segments:
|
49
|
+
- 0
|
50
|
+
version: "0"
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.3.7
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: Array Extention
|
67
|
+
test_files: []
|
68
|
+
|