Skip to content

Commit 92c84f0

Browse files
authored
Inheritance (sub class from Unit) no longer works (#340)
1 parent bb198f8 commit 92c84f0

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

.rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ Style/FormatString:
2424
Enabled: false
2525
Style/DateTime:
2626
Enabled: false
27+
Metrics/ClassLength:
28+
Enabled: false

Gemfile.lock

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ GEM
175175
PLATFORMS
176176
arm64-darwin-21
177177
arm64-darwin-22
178+
arm64-darwin-23
178179
java
179180
universal-java-11
180181
universal-java-18
@@ -203,4 +204,4 @@ DEPENDENCIES
203204
yard
204205

205206
BUNDLED WITH
206-
2.4.19
207+
2.5.3

lib/ruby_units/unit.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'date'
22
module RubyUnits
3-
# Copyright 2006-2023
3+
# Copyright 2006-2024
44
# @author Kevin C. Olbrich, Ph.D.
55
# @see https://github.com/olbrich/ruby-units
66
#
@@ -165,6 +165,17 @@ class << self
165165

166166
# Class Methods
167167

168+
# Callback triggered when a subclass is created. This properly sets up the internal variables, and copies
169+
# definitions from the parent class.
170+
#
171+
# @param [Class] subclass
172+
def self.inherited(subclass)
173+
super
174+
subclass.definitions = definitions.dup
175+
subclass.instance_variable_set(:@kinds, @kinds.dup)
176+
subclass.setup
177+
end
178+
168179
# setup internal arrays and hashes
169180
# @return [Boolean]
170181
def self.setup

spec/ruby_units/subclass_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
RSpec.describe 'Subclass' do
2+
subject(:subclass) { Class.new(RubyUnits::Unit) }
3+
4+
it 'can be subclassed' do
5+
expect(subclass).to be < RubyUnits::Unit
6+
end
7+
8+
it 'can be instantiated' do
9+
expect(subclass.new('1 m')).to be_a(RubyUnits::Unit)
10+
end
11+
12+
it 'compares to the parent class' do
13+
expect(subclass.new('1 m')).to eq(RubyUnits::Unit.new('1 m'))
14+
end
15+
16+
it 'can be added to another subclass instance' do
17+
expect(subclass.new('1 m') + subclass.new('1 m')).to eq(RubyUnits::Unit.new('2 m'))
18+
end
19+
20+
it 'returns a subclass object when added to another instance of a subclass' do
21+
expect(subclass.new('1 m') + subclass.new('1 m')).to be_an_instance_of(subclass)
22+
end
23+
24+
it 'returns an instance of the parent class when added to another instance of a subclass' do
25+
expect(RubyUnits::Unit.new('1 m') + subclass.new('1 m')).to be_an_instance_of(RubyUnits::Unit)
26+
end
27+
28+
it 'returns an instance of the subclass when added to an instance of the parent class' do
29+
expect(subclass.new('1 m') + RubyUnits::Unit.new('1 m')).to be_an_instance_of(subclass)
30+
end
31+
end

0 commit comments

Comments
 (0)