Skip to content

Commit c9a9d58

Browse files
aardvark179chrisseaton
authored andcommitted
Fix to_h specs.
1 parent e5c9030 commit c9a9d58

File tree

11 files changed

+53
-38
lines changed

11 files changed

+53
-38
lines changed

spec/tags/core/array/to_h_tags.txt

Lines changed: 0 additions & 5 deletions
This file was deleted.

spec/tags/core/enumerable/to_h_tags.txt

Lines changed: 0 additions & 5 deletions
This file was deleted.

spec/tags/core/env/to_h_tags.txt

Lines changed: 0 additions & 5 deletions
This file was deleted.

spec/tags/core/hash/to_h_tags.txt

Lines changed: 0 additions & 5 deletions
This file was deleted.

spec/tags/core/struct/to_h_tags.txt

Lines changed: 0 additions & 5 deletions
This file was deleted.

spec/tags/library/openstruct/to_h_tags.txt

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/main/ruby/core/array.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,8 +1183,22 @@ def to_ary
11831183
self
11841184
end
11851185

1186-
def to_h &block
1187-
super(*self, &block)
1186+
def to_h
1187+
h = Hash.new(size)
1188+
each_with_index do |elem, i|
1189+
elem = yield(elem) if block_given?
1190+
unless elem.respond_to?(:to_ary)
1191+
raise TypeError, "wrong element type #{elem.class} at #{i} (expected array)"
1192+
end
1193+
1194+
ary = elem.to_ary
1195+
if ary.size != 2
1196+
raise ArgumentError, "wrong array length at #{i} (expected 2, was #{ary.size})"
1197+
end
1198+
1199+
h[ary[0]] = ary[1]
1200+
end
1201+
h
11881202
end
11891203

11901204
def transpose

src/main/ruby/core/enumerable.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,14 @@ def to_a(*arg)
283283
def to_h(*arg)
284284
h = {}
285285
each_with_index(*arg) do |elem, i|
286+
elem = yield(elem) if block_given?
286287
unless elem.respond_to?(:to_ary)
287288
raise TypeError, "wrong element type #{elem.class} at #{i} (expected array)"
288289
end
289290

290291
ary = elem.to_ary
291292
if ary.size != 2
292-
raise ArgumentError, "wrong array length at #{i} (expected 2, was #{ary.size})"
293+
raise ArgumentError, "element has wrong array length (expected 2, was #{ary.size})"
293294
end
294295

295296
h[ary[0]] = ary[1]

src/main/ruby/core/env.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,23 @@ def to_a
260260
end
261261

262262
def to_hash
263-
hsh = {}
264-
each { |k, v| hsh[k] = v }
265-
hsh
263+
h = {}
264+
each_with_index do |elem|
265+
if block_given?
266+
elem = yield(elem)
267+
end
268+
unless elem.respond_to?(:to_ary)
269+
raise TypeError, "wrong element type #{elem.class} (expected array)"
270+
end
271+
272+
ary = elem.to_ary
273+
if ary.size != 2
274+
raise ArgumentError, "element has wrong array length (expected 2, was #{ary.size})"
275+
end
276+
277+
h[ary[0]] = ary[1]
278+
end
279+
h
266280
end
267281

268282
alias_method :to_h, :to_hash

src/main/ruby/core/hash.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,9 @@ def select!
298298
end
299299

300300
def to_h
301-
if instance_of? Hash
301+
if block_given?
302+
super
303+
elsif instance_of? Hash
302304
self
303305
else
304306
Hash.allocate.replace(to_hash)

0 commit comments

Comments
 (0)