Sha256: 48008e20a778344e7e47de96cf92641bf3fd199daffbe65663435be073c4650e

Contents?: true

Size: 1.61 KB

Versions: 49

Compression:

Stored size: 1.61 KB

Contents

# Subclassed From Core Class

## Introduction

Candidate classes for the _Subclassed From Core Class_ smell are classes which inherit from Core Classes like Hash, String and Array.

Inheriting from Core Classes means that you are going to have a bad time debugging (the explanation below is taken from [here](http://words.steveklabnik.com/beware-subclassing-ruby-core-classes)):

> What do you think this code should do?

```Ruby
List = Class.new(Array)

l = List.new
l << 1
l << 2
puts l.reverse.class # => Array
```

> If you said “it prints Array” you’d be right.
> Let’s talk about a more pernicious issue: Strings.

```Ruby
class MyString < String
  def to_s
    "lol"
  end
end

s = MyString.new
s.concat "Hey"

puts s      # => Hey
puts s.to_s # => lol
puts "#{s}" # => Hey
```

> That’s right! With Strings, Ruby doesn’t call #to_s: it puts the value in directly.
> Generally speaking, subclassing isn’t the right idea here.

## Example

Given

```Ruby
class Ary < Array
end

class Str < String
end
```

Reek would report the _Subclassed From Core Class_ smell for both classes. Instead of subclassing them you want a data structure that uses one of these core classes internally, but isn’t exactly like one. For instance:

```Ruby
require 'forwardable'

class List
  extend Forwardable
  def_delegators :@list, :<<, :length # and anything else

  def initialize(list = [])
    @list = list
  end

  def reverse
    List.new(@list.reverse)
  end
end

l = List.new 
l << 1
l << 2
puts l.reverse.class  # => List
```

## Configuration

_Subclassed From Core Class_ offers the [Basic Smell Options](Basic-Smell-Options.md).

Version data entries

49 entries across 47 versions & 2 rubygems

Version Path
reek-6.0.3 docs/Subclassed-From-Core-Class.md
reek-6.0.2 docs/Subclassed-From-Core-Class.md
reek-6.0.1 docs/Subclassed-From-Core-Class.md
reek-6.0.0 docs/Subclassed-From-Core-Class.md
reek-5.6.0 docs/Subclassed-From-Core-Class.md
reek-5.5.0 docs/Subclassed-From-Core-Class.md
reek-5.4.1 docs/Subclassed-From-Core-Class.md
reek-5.4.0 docs/Subclassed-From-Core-Class.md
reek-5.3.2 docs/Subclassed-From-Core-Class.md
reek-5.3.1 docs/Subclassed-From-Core-Class.md
reek-5.3.0 docs/Subclassed-From-Core-Class.md
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/reek-5.2.0/docs/Subclassed-From-Core-Class.md
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/reek-5.2.0/docs/Subclassed-From-Core-Class.md
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/reek-5.2.0/docs/Subclassed-From-Core-Class.md
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/reek-5.2.0/docs/Subclassed-From-Core-Class.md
reek-5.2.0 docs/Subclassed-From-Core-Class.md
reek-5.1.0 docs/Subclassed-From-Core-Class.md
reek-5.0.2 docs/Subclassed-From-Core-Class.md
reek-5.0.1 docs/Subclassed-From-Core-Class.md
reek-5.0.0 docs/Subclassed-From-Core-Class.md