Feature #8449

Array#ary_plus always returns an array

Added by José M. González over 2 years ago. Updated over 2 years ago.

Assignee:Nobuyoshi Nakada



Even when Array is subclassified, + operator keeps on returning an Array.

Don't know if it is a bug or intentional, but it has surprised me for sure.

I paste some code for demonstrational purposes:

#!/usr/bin/env ruby

# Shows an unexpected behavior to me
# when adding Array subclasess.

class Subarray < Array; end

a = Subarray.new
b = Subarray.new

result = a + b
# I was expecting to have a Subarray back

unless result.is_a? Subarray
puts "result is not a [Subarray]"

puts "result variable is a [#{result.class.name}]"



#1 Updated by Zachary Scott over 2 years ago

  • Category set to core
  • Assignee set to Nobuyoshi Nakada

For what it's worth this happens with String too:

class Substring < String; end
=> nil
a = Substring.new
=> ""
b = Substring.new
=> ""
result = a + b
=> ""
=> String

Maybe nobu can tell us

Also available in: Atom PDF