Class: Array
- Inherits:
-
Object
show all
- Defined in:
- lib/ddbcli/ddb-rubyext.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
34
35
36
37
38
39
40
41
42
|
# File 'lib/ddbcli/ddb-rubyext.rb', line 34
def method_missing(method_name, *args, &block)
case method_name.to_s
when /=\Z/
self.each {|i| i[method_name.to_s.sub(/=\Z/, '')] = *args }
self
else
self.map {|i| i[method_name.to_s] }
end
end
|
Instance Method Details
#avg ⇒ Object
6
7
8
|
# File 'lib/ddbcli/ddb-rubyext.rb', line 6
def avg
self.sum / self.length
end
|
#group_by(name, &block) ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/ddbcli/ddb-rubyext.rb', line 10
def group_by(name, &block)
item_h = {}
self.each do |item|
key = item[name.to_s]
item_h[key] ||= []
item_h[key] << item
end
return item_h unless block
new_item_h = {}
item_h.each do |key, item_list|
if block.arity == 2
new_item_h[key] = block.call(item_listm key)
else
new_item_h[key] = block.call(item_list)
end
end
return new_item_h
end
|
#sum ⇒ Object
2
3
4
|
# File 'lib/ddbcli/ddb-rubyext.rb', line 2
def sum
self.inject {|r, i| r + i }
end
|