Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/zmb/utils.rb
Instance Method Summary collapse
- #avg ⇒ Object
- #list_count ⇒ Object
- #list_join ⇒ Object
- #split_seperators ⇒ Object
- #sum ⇒ Object
- #word_count(seperator = '') ⇒ Object
Instance Method Details
#avg ⇒ Object
107 108 109 |
# File 'lib/zmb/utils.rb', line 107 def avg sum / count end |
#list_count ⇒ Object
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/zmb/utils.rb', line 92 def list_count items = {} each do |i| items[i] = 0 unless items.has_key?(i) items[i] += 1 end items.map{ |i, c| c == 1 ? i : "#{i} (#{c})" }.join(', ') end |
#list_join ⇒ Object
88 89 90 |
# File 'lib/zmb/utils.rb', line 88 def list_join (size > 2 ? [slice(0..-2).join(', '), last] : self).join(' and ') end |
#split_seperators ⇒ Object
80 81 82 |
# File 'lib/zmb/utils.rb', line 80 def split_seperators self end |
#sum ⇒ Object
103 104 105 |
# File 'lib/zmb/utils.rb', line 103 def sum inject {|result, element| result + element} end |
#word_count(seperator = '') ⇒ Object
84 85 86 |
# File 'lib/zmb/utils.rb', line 84 def word_count(seperator='') join(seperator).size end |