Last active
August 22, 2019 19:56
-
-
Save ixti/b035c9b1988be13de34dda8f677df167 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def print_result(desc) | |
| result = yield | |
| puts "#{desc} class=#{result.class} inspect=#{result.inspect}" | |
| rescue => e | |
| warn "#{desc} fail: #{e}" | |
| end | |
| def test(obj) | |
| puts "\n== #{obj.inspect} == " | |
| print_result("[*obj] =>") { [*obj] } | |
| print_result("Array(obj) =>") { Array(obj) } | |
| print_result("Array.wrap(obj) =>") { Array.wrap(obj) } | |
| end | |
| # trivial cases work the same: | |
| [nil, true, false, 161, [1, 3, 1, 2], { :foo => :bar }].each(&method(:test)) | |
| # things become different with objects that implement implicit (to_ary) and/or | |
| # explicit (to_a) coercions: | |
| test(1..3) | |
| # now, let's go crazy: | |
| class Dummy | |
| def initialize(to_a:, to_ary:) | |
| @to_a = to_a | |
| @to_ary = to_ary | |
| define_singleton_method(:to_a) { @to_a } if @to_a | |
| define_singleton_method(:to_ary) { @to_ary } if @to_ary | |
| end | |
| class << self | |
| alias [] new | |
| end | |
| end | |
| test(Dummy[:to_a => [1, 6, 1], :to_ary => [1, 3, 1, 2]]) | |
| test(Dummy[:to_a => "AFA", :to_ary => [1, 3, 1, 2]]) | |
| test(Dummy[:to_a => [1, 6, 1], :to_ary => "ACAB"]) | |
| test(Dummy[:to_a => [1, 6, 1], :to_ary => nil]) | |
| test(Dummy[:to_a => nil, :to_ary => [1, 6, 1]]) | |
| test(Dummy[:to_a => "AFA", :to_ary => nil]) | |
| test(Dummy[:to_a => nil, :to_ary => "AFA"]) | |
| test(Dummy[:to_a => nil, :to_ary => nil]) |
Author
Seems like Array.wrap is the most obvious implementation.
Author
To make it easier to think of those methods, here's brief outline of behaviours:
Array(object)
- Return
objectas is if it's anArray - Otherwise coerce object implicitly (if
objectresponds to#to_ary)- Raise
TypeErrorif coercion returned in non-Array
- Raise
- Otherwise coerce object explicitly (if
objectresponds to#to_a)- Raise
TypeErrorif coercion returned non-Array
- Raise
- Otherwise wrap
objectinto anArray(as in:[object])
Array.wrap(object)
- Return
objectas is if it's anArray - Otherwise coerce object implicitly (if
objectresponds to#to_ary)- Return result of coercion as is if it's truthy (anything but
nilorfalse)
- Return result of coercion as is if it's truthy (anything but
- Otherwise wrap
objectinto anArray(as in:[object])
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update 1: How
[*obj]behaves:#to_aryimplementation#to_a#to_aif object responds to thatTypeErrorif returned value isn't an Array