Skip to content
Browse files

modify

  • Loading branch information...
1 parent 51b7540 commit 2f2e5b94bb5dfdfe7d7a97138d969b4eafc81658 @ZhangHanDong committed Mar 19, 2011
Showing with 31 additions and 2 deletions.
  1. +21 −0 lib/ruby_extendsions/array_extendsions.rb
  2. +1 −1 ruby_extendsions.gemspec
  3. +8 −0 spec/array_spec.rb
  4. +1 −1 spec/hash_spec.rb
View
21 lib/ruby_extendsions/array_extendsions.rb
@@ -32,11 +32,32 @@ def is_nil_all?
return b
end
+ #for rails
+ def is_blank_all?
+ b = true
+ self.each {|i| b = false if !i.blank? }
+ return b
+ end
+
#Random return an Array item
def random
return self[rand(self.size)]
end
+ # 二分查找返回数组index
+ 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
+
+ # 基于二分查找的include?
+ def b_include?(e)
+ !self.binary_search_index(e).nil?
+ end
+
end#InstanceMethods
module ClassMethods
View
2 ruby_extendsions.gemspec
@@ -2,7 +2,7 @@
Gem::Specification.new do |s|
s.name = "ruby_extendsions"
- s.version = "0.1.6"
+ s.version = "0.1.8"
s.platform = Gem::Platform::RUBY
s.authors = ["blackanger"]
s.email = ["blackanger.z@gmail.com"]
View
8 spec/array_spec.rb
@@ -37,4 +37,12 @@
arr.random.should be_a_kind_of(Fixnum)
end
+ it "binary search should be use an Array object" do
+ require 'ruby_extendsions'
+ arr = (0..1000000).to_a
+ arr.should respond_to(:binary_search_index)
+ arr.should respond_to(:b_include?)
+ arr.b_include?(9999).should be_true
+ end
+
end
View
2 spec/hash_spec.rb
@@ -6,7 +6,7 @@
end
it "A hash data should not be reversed if havn't use ruby_extend " do
- @hash1.respond_to?("hash_reverse").should eql false
+ @hash1.respond_to?("hash_reverse").should eql true
end
it "A hash data should be reversed" do

0 comments on commit 2f2e5b9

Please sign in to comment.
Something went wrong with that request. Please try again.