Module: RubyExtendsions::ArrayExtendsions::InstanceMethods
- Defined in:
- lib/ruby_extendsions/array_extendsions.rb
Overview
extend ActiveSupport::Concern
Instance Method Summary collapse
-
#b_include?(e) ⇒ Boolean
基于二分查找的include?.
-
#binary_search_index(e, l = 0, u = length - 1) ⇒ Object
二分查找返回数组index.
-
#compare_content(com_arr) ⇒ Object
比较两个数组内容相同.
- #fill_nil(val) ⇒ Object
- #fill_nil!(val) ⇒ Object
-
#format_mess_insert ⇒ Object
change [[1,2,3][4,5,6]] to (‘1’,‘2’,‘3’),(‘4’,‘5’,‘6’) be used with mess insert mysql.
-
#is_blank_all? ⇒ Boolean
for rails.
-
#is_nil_all? ⇒ Boolean
whether the array is all nil item like arr = [nil,nil,nil,nil] arr.is_nil_all? #=> true.
-
#random ⇒ Object
Random return an Array item.
Instance Method Details
#b_include?(e) ⇒ Boolean
基于二分查找的include?
57 58 59 |
# File 'lib/ruby_extendsions/array_extendsions.rb', line 57 def b_include? e !self.binary_search_index(e).nil? end |
#binary_search_index(e, l = 0, u = length - 1) ⇒ Object
二分查找返回数组index
48 49 50 51 52 53 54 |
# File 'lib/ruby_extendsions/array_extendsions.rb', line 48 def binary_search_index(e, l = 0, u = length - 1) return if l>u m=(l+u)/2 return if !(e.class == self[m].class) e < self[m] ? u=m-1 : l=m+1 e == self[m] ? m : binary_search_index(e,l,u) end |
#compare_content(com_arr) ⇒ Object
比较两个数组内容相同
62 63 64 65 66 |
# File 'lib/ruby_extendsions/array_extendsions.rb', line 62 def compare_content com_arr arr = Set.new self com_arr = Set.new com_arr return arr == com_arr end |
#fill_nil(val) ⇒ Object
10 11 12 |
# File 'lib/ruby_extendsions/array_extendsions.rb', line 10 def fill_nil(val) self.inject([]) {|s, a| s << (a.nil? ? a=val : a)} end |
#fill_nil!(val) ⇒ Object
14 15 16 |
# File 'lib/ruby_extendsions/array_extendsions.rb', line 14 def fill_nil!(val) self.each_index { |i| self[i] ||= val } end |
#format_mess_insert ⇒ Object
change [[1,2,3][4,5,6]] to (‘1’,‘2’,‘3’),(‘4’,‘5’,‘6’) be used with mess insert mysql
20 21 22 23 24 25 |
# File 'lib/ruby_extendsions/array_extendsions.rb', line 20 def format_mess_insert self.inject('') do |s,i| t = i.inject('(') { |x, n| x << "'#{n}'," }.chop s << t << '),' end.chop end |
#is_blank_all? ⇒ Boolean
for rails
37 38 39 40 41 |
# File 'lib/ruby_extendsions/array_extendsions.rb', line 37 def is_blank_all? b = true self.each {|i| b = false if !i.blank? } return b end |
#is_nil_all? ⇒ Boolean
whether the array is all nil item like arr = [nil,nil,nil,nil] arr.is_nil_all? #=> true
30 31 32 33 34 |
# File 'lib/ruby_extendsions/array_extendsions.rb', line 30 def is_nil_all? b = true self.each {|i| b = false if !i.nil? } return b end |
#random ⇒ Object
Random return an Array item
44 45 46 |
# File 'lib/ruby_extendsions/array_extendsions.rb', line 44 def random return self[rand(self.size)] end |