Class: Array
- Defined in:
- lib/ditzstr/util.rb,
lib/ditzstr/util.rb,
lib/ditzstr/lowline.rb
Instance Method Summary collapse
- #first_duplicate ⇒ Object
- #flatten_one_level ⇒ Object
- #listify(prefix = "") ⇒ Object
- #to_h ⇒ Object
- #uniq_by ⇒ Object
Instance Method Details
#first_duplicate ⇒ Object
41 42 43 44 |
# File 'lib/ditzstr/util.rb', line 41 def first_duplicate sa = sort (1 .. sa.length).argfind { |i| (sa[i] == sa[i - 1]) && sa[i] } end |
#flatten_one_level ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ditzstr/util.rb', line 50 def flatten_one_level inject([]) do |ret, e| case e when Array ret + e else ret << e end end end |
#listify(prefix = "") ⇒ Object
23 24 25 26 27 28 |
# File 'lib/ditzstr/lowline.rb', line 23 def listify prefix="" return "" if empty? "\n" + map_with_index { |x, i| x.to_s.gsub(/^/, "#{prefix}#{i + 1}. ") }. join("\n") end |
#to_h ⇒ Object
46 47 48 |
# File 'lib/ditzstr/util.rb', line 46 def to_h Hash[*flatten] end |
#uniq_by ⇒ Object
12 |
# File 'lib/ditzstr/util.rb', line 12 def uniq_by; inject({}) { |h, o| h[yield(o)] = o; h }.values end |