backports 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -1
- data/VERSION.yml +1 -1
- data/lib/backports/array.rb +0 -1
- data/lib/backports/enumerable.rb +13 -4
- data/test/backports_test.rb +4 -0
- metadata +1 -1
data/README.rdoc
CHANGED
data/VERSION.yml
CHANGED
data/lib/backports/array.rb
CHANGED
data/lib/backports/enumerable.rb
CHANGED
@@ -11,10 +11,19 @@ module Enumerable
|
|
11
11
|
end unless method_defined? :sum
|
12
12
|
|
13
13
|
# Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
|
14
|
-
def find_index(
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
def find_index(*args)
|
15
|
+
if args.size == 1
|
16
|
+
obj = args.first
|
17
|
+
each_with_index do |element, i|
|
18
|
+
return i if element == obj
|
19
|
+
end
|
20
|
+
elsif block_given?
|
21
|
+
each_with_index do |element, i|
|
22
|
+
return i if yield element
|
23
|
+
end
|
24
|
+
each_with_index{|o,i| return i if yield o}
|
25
|
+
else
|
26
|
+
raise ArgumentError, "Wrong number of arguments (#{args.size} for 1)"
|
18
27
|
end
|
19
28
|
nil
|
20
29
|
end unless method_defined? :find_index
|
data/test/backports_test.rb
CHANGED
@@ -6,6 +6,10 @@ class BackportsTest < Test::Unit::TestCase
|
|
6
6
|
assert_equal 3, %w{ant bat cat dog}.find_index {|item| item =~ /g/ }
|
7
7
|
assert_equal nil, %w{ant bat cat dog}.find_index {|item| item =~ /h/ }
|
8
8
|
end
|
9
|
+
|
10
|
+
should "work for enumerables too" do
|
11
|
+
assert_equal 69-42, (42..666).find_index(69)
|
12
|
+
end
|
9
13
|
end
|
10
14
|
|
11
15
|
context "take" do
|