equal_rights_for_hash 0.0.1
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 +4 -0
- data/equal_rights_for_hash.gemspec +16 -0
- data/lib/equal_rights_for_hash.rb +25 -0
- data/test/kernel_hash_test.rb +29 -0
- data/test/to_h_test.rb +44 -0
- metadata +53 -0
data/README
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
Gem::Specification.new do |s|
|
3
|
+
s.name = "equal_rights_for_hash"
|
4
|
+
s.version = "0.0.1"
|
5
|
+
s.authors = ["Suraj N. Kurapati"]
|
6
|
+
s.email = ["sunaku@gmail.com"]
|
7
|
+
s.homepage = "http://redmine.ruby-lang.org/issues/5008"
|
8
|
+
s.summary = %q{Adds Kernel#Hash() and generic #to_h() methods.}
|
9
|
+
s.description = %q{Grants equal rights for Hash like other Ruby data
|
10
|
+
structures: Array, String, Integer, and Float.}
|
11
|
+
|
12
|
+
s.files = `git ls-files`.split("\n")
|
13
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
15
|
+
s.require_paths = ["lib"]
|
16
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Kernel
|
2
|
+
def Hash(value)
|
3
|
+
value.to_h
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
class Object
|
8
|
+
def to_h
|
9
|
+
if respond_to? :to_hash
|
10
|
+
to_hash
|
11
|
+
elsif respond_to? :to_ary and not all? {|item|
|
12
|
+
item.respond_to? :to_ary and item.size <= 2 }
|
13
|
+
then
|
14
|
+
Hash[*self]
|
15
|
+
else
|
16
|
+
Hash[self]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class NilClass
|
22
|
+
def to_h
|
23
|
+
{}
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
|
3
|
+
require "equal_rights_for_hash"
|
4
|
+
|
5
|
+
class TestKernelHash < Test::Unit::TestCase
|
6
|
+
def test_converts_nil_into_hash
|
7
|
+
assert_equal({}, Hash(nil))
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_converts_real_hash_into_self
|
11
|
+
real_hash = {:real => true}
|
12
|
+
assert_same(real_hash, Hash(real_hash))
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_converts_fake_hash_into_hash
|
16
|
+
fake_hash = Object.new
|
17
|
+
def fake_hash.to_hash
|
18
|
+
{:fake => true}
|
19
|
+
end
|
20
|
+
assert_kind_of(Hash, Hash(fake_hash))
|
21
|
+
assert_equal({:fake => true}, Hash(fake_hash))
|
22
|
+
assert_equal(fake_hash.to_hash, Hash(fake_hash))
|
23
|
+
assert_equal(fake_hash.to_h, Hash(fake_hash))
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_converts_empty_array_into_hash
|
27
|
+
assert_equal({}, Hash([]))
|
28
|
+
end
|
29
|
+
end
|
data/test/to_h_test.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
|
3
|
+
require "equal_rights_for_hash"
|
4
|
+
|
5
|
+
class TestToH < Test::Unit::TestCase
|
6
|
+
def test_converts_nil_into_hash
|
7
|
+
assert_equal({}, nil.to_h)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_converts_real_hash_into_self
|
11
|
+
real_hash = {:real => true}
|
12
|
+
assert_same(real_hash, real_hash.to_h)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_converts_fake_hash_into_hash
|
16
|
+
fake_hash = Object.new
|
17
|
+
def fake_hash.to_h
|
18
|
+
{:fake => true}
|
19
|
+
end
|
20
|
+
assert_kind_of(Hash, fake_hash.to_h)
|
21
|
+
assert_equal({:fake => true}, fake_hash.to_h)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_converts_empty_array_into_hash
|
25
|
+
assert_equal({}, [].to_h)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_converts_flat_even_array_into_hash
|
29
|
+
assert_equal({1 => 2, 3 => 4}, [1, 2, 3, 4].to_h)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_converts_nested_even_array_into_hash
|
33
|
+
assert_equal({1 => 2, 3 => 4}, [[1, 2], [3, 4]].to_h)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_converts_flat_odd_array_into_hash
|
37
|
+
error = assert_raise(ArgumentError) { [1, 2, 3].to_h }
|
38
|
+
assert_equal("odd number of arguments for Hash", error.message)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_converts_nested_odd_array_into_hash
|
42
|
+
assert_equal({1 => 2, 3 => nil}, [[1, 2], [3]].to_h)
|
43
|
+
end
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: equal_rights_for_hash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Suraj N. Kurapati
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-27 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! "Grants equal rights for Hash like other Ruby data\n structures:
|
15
|
+
Array, String, Integer, and Float."
|
16
|
+
email:
|
17
|
+
- sunaku@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- README
|
23
|
+
- equal_rights_for_hash.gemspec
|
24
|
+
- lib/equal_rights_for_hash.rb
|
25
|
+
- test/kernel_hash_test.rb
|
26
|
+
- test/to_h_test.rb
|
27
|
+
homepage: http://redmine.ruby-lang.org/issues/5008
|
28
|
+
licenses: []
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
requirements: []
|
46
|
+
rubyforge_project:
|
47
|
+
rubygems_version: 1.8.10
|
48
|
+
signing_key:
|
49
|
+
specification_version: 3
|
50
|
+
summary: ! 'Adds Kernel#Hash() and generic #to_h() methods.'
|
51
|
+
test_files:
|
52
|
+
- test/kernel_hash_test.rb
|
53
|
+
- test/to_h_test.rb
|