Class: Array
- Inherits:
-
Object
show all
- Defined in:
- lib/classes/arrays.rb
Overview
my personal Array powerup class
Instance Method Summary
collapse
-
#append(str) ⇒ Object
-
#average ⇒ Object
(also: #avg)
-
#chomp_all ⇒ Object
-
#chop_all ⇒ Object
-
#color(regex, opts = {}) ⇒ Object
-
#double_quote ⇒ Object
-
#duplicates(count = false) ⇒ Object
(also: #doppioni)
find duplicates if count, does what uniq -c does (returns occurrences as well).
-
#egrep(regex) ⇒ Object
-
#egrep_v(regex) ⇒ Object
-
#evidenzia(string_or_array_or_regex, opts = {}) ⇒ Object
string must NOT contain //, i.e.
-
#from(el) ⇒ Object
EL should be an EXACT match, NOT a regex :(.
-
#from_to(el1, el2) ⇒ Object
-
#head ⇒ Object
-
#index_regex(regex) ⇒ Object
finds index of regex, to be sure I stringify everything.
-
#join2(initial, final) ⇒ Object
[1,2,3].join2(‘- ’,“n”) => “- 1n- 2n- 3n”.
-
#join3(initial = "'", middle = "', '", final = "'") ⇒ Object
-
#map_to_hash ⇒ Object
-
#match(regex) ⇒ Object
(also: #matches)
-
#method_missing(method_name, *args) ⇒ Object
copiato da srtsolution.com/public/blog/250508.
-
#names ⇒ Object
it would be awesome with ‘method_not_existent’ to mark all the plurals into a singular call of the elements :).
-
#prepend(str) ⇒ Object
-
#quote(sep = nil) ⇒ Object
-
#quote_all ⇒ Object
-
#remove_last ⇒ Object
-
#remove_options ⇒ Object
naive removal of ARGV options :) ARGV: “-h -dn -aaa A B C” –> “A B C”.
-
#scalar_product(arr2) ⇒ Object
(also: #mul)
-
#smart_printf(opts = {}) ⇒ Object
options: :first_line_as_title (make it bold) smart printf of an AoA like [[ title1, title2, title3], [ arg1 arg2 arg3 ] .… printf with perfect %10d length.
-
#sum ⇒ Object
-
#tail ⇒ Object
-
#to(el) ⇒ Object
-
#to_s ⇒ Object
-
#to_s_according_to_ruby ⇒ Object
-
#trim_all ⇒ Object
(also: #trim)
-
#uniq_c(min_cardinality = 1) ⇒ Object
like UNIQ - C, returns uncity with cardinality into an elegant HASH :) sort is pointless.
-
#weighted_average(weights) ⇒ Object
(also: #wavg)
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
copiato da srtsolution.com/public/blog/250508
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
# File 'lib/classes/arrays.rb', line 220
def method_missing(method_name, *args)
method_name = method_name.to_s
deb "Array.method_missing: called '#{method_name}' (class=#{method_name.class}) with args: #{ (args * ',') }"
if m = method_name.match( /^(.*)_all$/)
descr = "Calling on this array this function: " + white(" .map{|x| x.#{m[1]}(#{args.map{|x| x.to_s.quote}.join(', ')}) }")
deb descr
deb "DEB Figoso: I call #{m[1]} on the Array son, as an object, like obj.foo(args)"
return self.map{|x| x.send(m[1], *args ) }
end
if method_name =~ /^map_(.*)$/
deb "method_name matches map_SOMETHING!"
return self.map{|x| x.send($1, *args ) }
end
if method_name =~ /^(.*)s$/
pazure "Cool! Seems like you called a plural attribute over an array. It would be *SO* cool to call map.#{$1} but I dont trust myself enough, so its better that you use: #{green "map_#{$1}"}"
return super end
return true
end
|
Instance Method Details
#append(str) ⇒ Object
136
137
138
|
# File 'lib/classes/arrays.rb', line 136
def append(str)
map{|x| x+str}
end
|
#average ⇒ Object
Also known as:
avg
111
112
113
|
# File 'lib/classes/arrays.rb', line 111
def average
sum / size
end
|
#chomp_all ⇒ Object
76
77
78
|
# File 'lib/classes/arrays.rb', line 76
def chomp_all
map{|x| x.chomp }
end
|
#chop_all ⇒ Object
139
140
141
|
# File 'lib/classes/arrays.rb', line 139
def chop_all
map{|x| x.chop }
end
|
#color(regex, opts = {}) ⇒ Object
184
185
186
187
188
189
190
191
|
# File 'lib/classes/arrays.rb', line 184
def color(regex, opts = {} )
opts[:color] ||= :red
map{|line|
line.gsub(regex) {|match|
match.color(opts[:color])
}
} rescue map{|line| line.gsub(/(b)/,green('b')) }
end
|
#double_quote ⇒ Object
73
74
75
|
# File 'lib/classes/arrays.rb', line 73
def double_quote()
map{|x| x.quote('"') }
end
|
#duplicates(count = false) ⇒ Object
Also known as:
doppioni
find duplicates if count, does what uniq -c does (returns occurrences as well)
214
215
216
|
# File 'lib/classes/arrays.rb', line 214
def duplicates(count = false)
count ? dups_c : dups
end
|
#egrep(regex) ⇒ Object
201
202
203
|
# File 'lib/classes/arrays.rb', line 201
def egrep(regex)
select{|line| line.match(regex) }
end
|
#egrep_v(regex) ⇒ Object
198
199
200
|
# File 'lib/classes/arrays.rb', line 198
def egrep_v(regex)
select{|line| ! line.match(regex) }
end
|
#evidenzia(string_or_array_or_regex, opts = {}) ⇒ Object
string must NOT contain //, i.e. ‘goliardia’ or “zuppa|pambagnato”
206
207
208
209
|
# File 'lib/classes/arrays.rb', line 206
def evidenzia(string_or_array_or_regex, opts = {} )
opts[:color] ||= :yellow
color(autoregex(string_or_array_or_regex), opts)
end
|
#from(el) ⇒ Object
EL should be an EXACT match, NOT a regex :(
37
38
39
40
41
42
|
# File 'lib/classes/arrays.rb', line 37
def from(el)
return [] unless self.include?(el)
ix1 = self.index(el)
ix2 = self.length
self[ix1..ix2]
end
|
#from_to(el1, el2) ⇒ Object
50
51
52
|
# File 'lib/classes/arrays.rb', line 50
def from_to(el1,el2)
from(el1).to(el2)
end
|
#head ⇒ Object
92
93
94
|
# File 'lib/classes/arrays.rb', line 92
def head
self[0] end
|
#index_regex(regex) ⇒ Object
finds index of regex, to be sure I stringify everything
241
242
243
244
|
# File 'lib/classes/arrays.rb', line 241
def index_regex(regex)
first_element = self.map{|x| x.to_s}.grep(regex).first
return index( first_element )
end
|
#join2(initial, final) ⇒ Object
[1,2,3].join2(‘- ’,“n”) => “- 1n- 2n- 3n”
55
56
57
|
# File 'lib/classes/arrays.rb', line 55
def join2(initial,final)
initial + self.join(final+initial) + final
end
|
#join3(initial = "'", middle = "', '", final = "'") ⇒ Object
58
59
60
|
# File 'lib/classes/arrays.rb', line 58
def join3(initial="'",middle="', '",final="'")
initial.to_s + self.join(middle.to_s) + final.to_s
end
|
#map_to_hash ⇒ Object
62
63
64
|
# File 'lib/classes/arrays.rb', line 62
def map_to_hash
map { |e| yield e }.inject({}) { |carry, e| carry.merge! e }
end
|
#match(regex) ⇒ Object
Also known as:
matches
79
80
81
82
83
84
85
|
# File 'lib/classes/arrays.rb', line 79
def match(regex)
each{|x|
ret = x.match(regex)
return ret if ret
}
return nil
end
|
#names ⇒ Object
it would be awesome with ‘method_not_existent’ to mark all the plurals into a singular call of the elements :)
67
68
69
|
# File 'lib/classes/arrays.rb', line 67
def names
map{|x| x.name }
end
|
#prepend(str) ⇒ Object
133
134
135
|
# File 'lib/classes/arrays.rb', line 133
def prepend(str)
map{|x| str+x}
end
|
#quote(sep = nil) ⇒ Object
70
71
72
|
# File 'lib/classes/arrays.rb', line 70
def quote(sep=nil)
map{|x| x.quote(sep) }
end
|
#quote_all ⇒ Object
145
146
147
|
# File 'lib/classes/arrays.rb', line 145
def quote_all
map{|x| x.quote }
end
|
#remove_last ⇒ Object
194
195
196
|
# File 'lib/classes/arrays.rb', line 194
def remove_last
return self[0.. self.size-2 ]
end
|
#remove_options ⇒ Object
naive removal of ARGV options :) ARGV: “-h -dn -aaa A B C” –> “A B C”
32
33
34
|
# File 'lib/classes/arrays.rb', line 32
def remove_options
select{|x| ! x.match( /^-/ ) }
end
|
#scalar_product(arr2) ⇒ Object
Also known as:
mul
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/classes/arrays.rb', line 116
def scalar_product(arr2)
throw "must be same size (self: #{size}) / arg: #{arr2.size})!!!" if( size != arr2.size )
ret = 0.0
for i in (0..size)
deb "DEB #{self[i]} * #{arr2[i]}"
ret += ( self[i].to_f * arr2[i].to_f)
end
ret
end
|
#smart_printf(opts = {}) ⇒ Object
options:
:first_line_as_title (make it bold)
smart printf of an AoA like [[ title1, title2, title3],
[ arg1 arg2 arg3 ]
.… printf with perfect %10d length
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/classes/arrays.rb', line 156
def smart_printf(opts={})
printf_template = ''
width=self[0].size
deb width
for k in (0 .. width-1) max = self.map{|line| line[k].to_s.length }.max
deb( [k, max].join(':'))
printf_template += "%-#{max}s "
end
printf_template += "\n"
deb("printf_template: #{printf_template}")
self.each{ |line|
puts( printf_template % line ) }
end
|
#sum ⇒ Object
103
104
105
106
107
108
109
|
# File 'lib/classes/arrays.rb', line 103
def sum
ret = 0
self.each{ |el|
ret += el.to_f
}
ret
end
|
#tail ⇒ Object
96
97
98
99
100
|
# File 'lib/classes/arrays.rb', line 96
def tail cp = Array.new(self)
cp.shift
return cp
end
|
#to(el) ⇒ Object
44
45
46
47
48
|
# File 'lib/classes/arrays.rb', line 44
def to(el)
return [] unless self.include?(el)
ix2 = self.index(el)
self[0..ix2]
end
|
#to_s ⇒ Object
26
27
28
|
# File 'lib/classes/arrays.rb', line 26
def to_s
"[ #{join(', ').to_s} ]"
end
|
#to_s_according_to_ruby ⇒ Object
24
|
# File 'lib/classes/arrays.rb', line 24
alias :to_s_according_to_ruby :to_s
|
#trim_all ⇒ Object
Also known as:
trim
142
143
144
|
# File 'lib/classes/arrays.rb', line 142
def trim_all
map{|x| x.trim }
end
|
#uniq_c(min_cardinality = 1) ⇒ Object
like UNIQ - C, returns uncity with cardinality into an elegant HASH :) sort is pointless
178
179
180
181
182
|
# File 'lib/classes/arrays.rb', line 178
def uniq_c(min_cardinality = 1) hash=Hash.new
self.map{|x| hash[x] = (hash[x] + 1) rescue 1 }
hash.select{|k,v| v >= min_cardinality}
end
|
#weighted_average(weights) ⇒ Object
Also known as:
wavg
128
129
130
|
# File 'lib/classes/arrays.rb', line 128
def weighted_average(weights)
scalar_product(weights) / weights.sum
end
|