ruby_extend 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +9 -1
- data/lib/ruby_extend.rb +38 -4
- data/test/test_ruby_extensions.rb +31 -2
- metadata +22 -42
data/README.rdoc
CHANGED
@@ -12,4 +12,12 @@ Extend Hash with to_sorted_array : Recursively sorted array from hash
|
|
12
12
|
Extend Array with #to_hash : Recursively convert array to hash
|
13
13
|
array = [["user", [["adress", [["street", "rue de Quimper"], ["town", "Paris"]]], ["age", 12], ["name", "toto"]]]]
|
14
14
|
array.to_hash
|
15
|
-
# =>{"user"=> {"name"=>"toto", "adress"=>{"street"=>"rue de Quimper", "town"=>"Paris"}, "age"=>12}}
|
15
|
+
# =>{"user"=> {"name"=>"toto", "adress"=>{"street"=>"rue de Quimper", "town"=>"Paris"}, "age"=>12}}
|
16
|
+
|
17
|
+
=== Release 2
|
18
|
+
Array#move(from,to)
|
19
|
+
Array#included_in?(array)
|
20
|
+
Array#duplicates
|
21
|
+
Array#map_with_index
|
22
|
+
Array#average
|
23
|
+
Array#longest
|
data/lib/ruby_extend.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
class Hash
|
2
|
-
|
2
|
+
|
3
3
|
# Return array from hash recursively sorted
|
4
4
|
def to_sorted_array block=nil
|
5
|
-
|
6
5
|
sort(&block).inject([]) do |array, (key, value)|
|
7
|
-
|
6
|
+
value = value.is_a?(Hash) ? value.to_sorted_array : value
|
7
|
+
array << [key, value]
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
11
|
end
|
12
12
|
|
13
13
|
class Array
|
14
|
-
|
14
|
+
require 'set'
|
15
15
|
# Recursively convert array to hash
|
16
16
|
def to_hash
|
17
17
|
inject({}) do |hash, (key, value)|
|
@@ -20,4 +20,38 @@ class Array
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
+
def move(from, to)
|
24
|
+
insert(to, delete_at(from))
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
def included_in? array
|
29
|
+
array.to_set.superset?(self.to_set)
|
30
|
+
end
|
31
|
+
|
32
|
+
def duplicates
|
33
|
+
uniq.map { |element| diff = (self.size - (self-[element]).size); (diff > 1) ? [element, diff] : nil}.compact
|
34
|
+
end
|
35
|
+
|
36
|
+
def map_with_index
|
37
|
+
result = []
|
38
|
+
each_with_index do |element, index|
|
39
|
+
result << yield(element, index)
|
40
|
+
end
|
41
|
+
result
|
42
|
+
end
|
43
|
+
|
44
|
+
def average
|
45
|
+
if self.respond_to?(:sum)
|
46
|
+
sum.to_f / self.count
|
47
|
+
else
|
48
|
+
self.inject(:+).to_f / self.count
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def longest
|
53
|
+
max = group_by(&:size).max
|
54
|
+
max.last.pop if max
|
55
|
+
end
|
56
|
+
|
23
57
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'test/unit'
|
2
|
-
require File.join(File.dirname(__FILE__),'../lib/
|
2
|
+
require File.join(File.dirname(__FILE__),'../lib/ruby_extend')
|
3
3
|
|
4
4
|
|
5
5
|
class TestRubyExtensions < Test::Unit::TestCase
|
@@ -25,6 +25,35 @@ class TestRubyExtensions < Test::Unit::TestCase
|
|
25
25
|
expected = {"user"=> {"name"=>"toto", "adress"=>{"street"=>"rue de Quimper", "town"=>"Paris"}, "age"=>12}}
|
26
26
|
assert_equal expected, array.to_hash
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
|
+
def test_array_included_in
|
30
|
+
assert [1, 3, 766, 9].included_in?([1, 3, 766, 9])
|
31
|
+
assert [1, 3, 766, 9].included_in?([1, 3, 766, 9,10])
|
32
|
+
assert ![1, 3, 4, 9].included_in?([1, 3, 766, 9])
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_move_an_element_of_an_array
|
36
|
+
a = [1, 2, 3, 4]
|
37
|
+
assert_equal [2, 3, 4, 1], a.move(0, 3)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_duplicates
|
41
|
+
assert_equal [[2, 2], [3, 3]], [1, 2, 4, 2, 3, 6, 3, 3].duplicates
|
42
|
+
assert_equal [], [1, 2, 3, 4].duplicates
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_map_with_index
|
46
|
+
[1, 2, 3, 4].map_with_index do |n, index|
|
47
|
+
assert_equal n, index + 1
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_average
|
52
|
+
assert_equal 10, [0, 20].average
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_longest
|
56
|
+
assert_equal "hello", ["hello", "how", "are", "you"].longest
|
57
|
+
end
|
29
58
|
|
30
59
|
end
|
metadata
CHANGED
@@ -1,69 +1,49 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_extend
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 1
|
8
|
-
- 0
|
9
|
-
- 0
|
10
|
-
version: 1.0.0
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
prerelease:
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Philippe Cantin
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
date: 2010-10-16 00:00:00 +02:00
|
19
|
-
default_executable:
|
12
|
+
date: 2012-06-23 00:00:00.000000000Z
|
20
13
|
dependencies: []
|
21
|
-
|
22
|
-
description: "Extend some ruby core classes "
|
14
|
+
description: Extend some ruby core classes
|
23
15
|
email:
|
24
16
|
executables: []
|
25
|
-
|
26
17
|
extensions: []
|
27
|
-
|
28
|
-
extra_rdoc_files:
|
18
|
+
extra_rdoc_files:
|
29
19
|
- README.rdoc
|
30
|
-
files:
|
20
|
+
files:
|
31
21
|
- lib/ruby_extend.rb
|
32
22
|
- README.rdoc
|
33
23
|
- test/test_ruby_extensions.rb
|
34
|
-
has_rdoc: true
|
35
24
|
homepage: http://github.com/anoiaque/ruby_extensions
|
36
25
|
licenses: []
|
37
|
-
|
38
26
|
post_install_message:
|
39
27
|
rdoc_options: []
|
40
|
-
|
41
|
-
require_paths:
|
28
|
+
require_paths:
|
42
29
|
- lib
|
43
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
31
|
none: false
|
45
|
-
requirements:
|
46
|
-
- -
|
47
|
-
- !ruby/object:Gem::Version
|
48
|
-
|
49
|
-
|
50
|
-
- 0
|
51
|
-
version: "0"
|
52
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ! '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
37
|
none: false
|
54
|
-
requirements:
|
55
|
-
- -
|
56
|
-
- !ruby/object:Gem::Version
|
57
|
-
|
58
|
-
segments:
|
59
|
-
- 0
|
60
|
-
version: "0"
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
61
42
|
requirements: []
|
62
|
-
|
63
43
|
rubyforge_project:
|
64
|
-
rubygems_version: 1.
|
44
|
+
rubygems_version: 1.8.16
|
65
45
|
signing_key:
|
66
46
|
specification_version: 3
|
67
47
|
summary: Extend some ruby core classes
|
68
|
-
test_files:
|
48
|
+
test_files:
|
69
49
|
- test/test_ruby_extensions.rb
|