Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/midwire_common/array.rb
Instance Method Summary collapse
-
#bsearch(e, l = 0, u = length - 1) ⇒ Object
Binary search returns index if found or nil rubocop:disable Metrics/LineLength rubocop:disable Style/SpaceAfterComma, Style/SpaceAroundOperators, Style/SpaceAfterColon, Style/SpaceAfterSemicolon, Style/Semicolon.
- #count_occurrences ⇒ Object
- #each_with_first_last(first_code, main_code, last_code) ⇒ Object
- #randomize ⇒ Object
- #randomize! ⇒ Object
- #sort_case_insensitive ⇒ Object
-
#superjoin(*ldescr) ⇒ Object
Make a string from a multi-dimensional array : 1 dimension : [1,2,3].superjoin().
Instance Method Details
#bsearch(e, l = 0, u = length - 1) ⇒ Object
Binary search returns index if found or nil rubocop:disable Metrics/LineLength rubocop:disable Style/SpaceAfterComma, Style/SpaceAroundOperators, Style/SpaceAfterColon, Style/SpaceAfterSemicolon, Style/Semicolon
33 34 35 |
# File 'lib/midwire_common/array.rb', line 33 def bsearch(e, l = 0, u = length - 1) return if l>u;m=(l+u)/2;e<self[m]?u=m-1:l=m+1;e==self[m]?m:bsearch(e,l,u) end |
#count_occurrences ⇒ Object
2 3 4 5 6 |
# File 'lib/midwire_common/array.rb', line 2 def count_occurrences k = Hash.new(0) each { |x| k[x] += 1 } k end |
#each_with_first_last(first_code, main_code, last_code) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/midwire_common/array.rb', line 20 def each_with_first_last(first_code, main_code, last_code) each_with_index do |item, i| case i when 0 then first_code.call(item) when size - 1 then last_code.call(item) else main_code.call(item) end end end |
#randomize ⇒ Object
8 9 10 |
# File 'lib/midwire_common/array.rb', line 8 def randomize sort_by { rand } end |
#randomize! ⇒ Object
12 13 14 |
# File 'lib/midwire_common/array.rb', line 12 def randomize! replace(randomize) end |
#sort_case_insensitive ⇒ Object
16 17 18 |
# File 'lib/midwire_common/array.rb', line 16 def sort_case_insensitive sort_by(&:downcase) end |
#superjoin(*ldescr) ⇒ Object
Make a string from a multi-dimensional array : 1 dimension : [1,2,3].superjoin()
2 dimensions: (html table) [[1,2],].superjoin( %w</tr><tr> </tr></table>, %w</td><td> </td> )
> <table><tr><td>1</td><td>2</td></tr><tr><td>2</td><td>3</td></tr></table>
45 46 47 48 49 50 51 |
# File 'lib/midwire_common/array.rb', line 45 def superjoin(*ldescr) d = ldescr[0] rest = ldescr[1..-1] d[0] + map do |a| (a.respond_to?(:superjoin) && rest.length > 0) ? a.superjoin(*rest) : a.to_s end.join(d[1]) + d[2] end |