backports 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -46,7 +46,7 @@ Works with ruby 1.8 & 1.9
46
46
  * +first+
47
47
  * Array
48
48
  * +flatten+, <tt>flatten!</tt>
49
- * +find_index+, +index+
49
+ * +find_index+, +find+
50
50
  * Fixnum
51
51
  * <tt>odd?</tt>, <tt>even?</tt>
52
52
 
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 1
3
3
  :minor: 1
4
- :patch: 0
4
+ :patch: 1
@@ -53,7 +53,6 @@ class Array
53
53
  each_with_index{|o,i| return i if yield o}
54
54
  return nil
55
55
  end
56
-
57
56
  alias_method_chain :index, :block
58
57
  alias_method :find_index, :index
59
58
  end
@@ -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(obj = nil)
15
- return index(obj) unless block_given?
16
- each_with_index do |element, i|
17
- return i if yield(element)
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
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: backports
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Marc-Andr\xC3\xA9 Lafortune"