Project

General

Profile

Actions

Feature #8449

open

Array#ary_plus always returns an array

Added by txemagon (José M. González) over 11 years ago. Updated 8 months ago.

Status:
Assigned
Target version:
-
[ruby-core:55162]

Description

=begin

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
#ary_plus.rb

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]"
end

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

=end

Updated by zzak (zzak _) over 11 years ago

  • Category set to core
  • Assignee set to nobu (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
=> ""
result.class
=> String

Maybe nobu can tell us

Actions #2

Updated by hsbt (Hiroshi SHIBATA) 8 months ago

  • Status changed from Open to Assigned
Actions

Also available in: Atom PDF

Like0
Like0Like0