From: Michael Schuerig Date: 2005-09-12T00:41:32+09:00 Subject: Re: When an Array is not David A. Black wrote: > Hi -- > > On Sun, 11 Sep 2005, Michael Schuerig wrote: > >> >> (1) Under what circumstances is an array not an Array? I'm confronted >> with such a case in one of my unit tests. The test failed when run in >> conjunction with other tests and I narrowed it down to this: In the >> tested code I use something.kind_of?(Array). I have a mixin module >> that extends Array with additional methods. When this module is >> required directly or indirectly in my test class then kind_of?(Array) >> doesn't behave as expected anymore. > > Can you provide a complete (non-)working example? I can't quite > reconstruct it from this. Attached below with resolution. When copying and pasting the minimally necessary pieces to recreate the failure I found the cause. The module where the failure occurs has a sibling module named Array. Ruby resolved the unadorned Array to this module, not the built-in class Array. Michael require 'test/unit' module BoilerPlate #:nodoc: module CoreExtensions #:nodoc: module Array #:nodoc: end end end module BoilerPlate # :nodoc: module CoreExtensions # :nodoc: module Hash # :nodoc: module Misc def value_to_a(key, default = []) if value = self[key] # ::Array is necessary to refer to the globally scoped Array # Array alone refers to the sibling module Array value.kind_of?(::Array) ? value : [ value ] else default end end end end end end Hash.class_eval do include BoilerPlate::CoreExtensions::Hash::Misc end class HashMiscTest < Test::Unit::TestCase def test_value_to_a_array options = { 'mykey' => [ 'thevalue', 'anothervalue' ] } assert_equal(['thevalue', 'anothervalue'], options.value_to_a('mykey')) end end -- Michael Schuerig Those people who smile a lot mailto:michael@schuerig.de Watch the eyes http://www.schuerig.de/michael/ --Ani DiFranco, Outta Me, Onto You