Skip to content

Commit 594e582

Browse files
committed
Improve to_s and inspection on atomic objects
1 parent 2a8a589 commit 594e582

File tree

7 files changed

+38
-1
lines changed

7 files changed

+38
-1
lines changed

lib/concurrent/atomic/atomic_boolean.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,11 @@ module Concurrent
112112
#
113113
# @!macro atomic_boolean_public_api
114114
class AtomicBoolean < AtomicBooleanImplementation
115+
# @return [String] Short string representation.
116+
def to_s
117+
format '<#%s:0x%x value:%s>', self.class, object_id << 1, value
118+
end
119+
120+
alias_method :inspect, :to_s
115121
end
116122
end

lib/concurrent/atomic/atomic_fixnum.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ module Concurrent
131131
class AtomicFixnum < AtomicFixnumImplementation
132132
# @return [String] Short string representation.
133133
def to_s
134-
format '<#%s:0x%x value:%s>', self.class, object_id << 1, get
134+
format '<#%s:0x%x value:%s>', self.class, object_id << 1, value
135135
end
136+
137+
alias_method :inspect, :to_s
136138
end
137139
end

lib/concurrent/atomic/atomic_reference.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,6 @@ class Concurrent::AtomicReference
4646
def to_s
4747
format '<#%s:0x%x value:%s>', self.class, object_id << 1, get
4848
end
49+
50+
alias_method :inspect, :to_s
4951
end

lib/concurrent/edge/lock_free_stack.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,7 @@ def clear_each(&block)
118118
def to_s
119119
format '<#%s:0x%x %s>', self.class, object_id << 1, to_a.to_s
120120
end
121+
122+
alias_method :inspect, :to_s
121123
end
122124
end

spec/concurrent/atomic/atomic_boolean_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,13 @@ module Concurrent
174174
expect(AtomicBoolean.ancestors).to include(MutexAtomicBoolean)
175175
end
176176
end
177+
178+
describe '#to_s and #inspect' do
179+
it 'includes the value' do
180+
subject = described_class.new(true)
181+
expect(subject.to_s).to include('true')
182+
expect(subject.inspect).to include('true')
183+
end
184+
end
177185
end
178186
end

spec/concurrent/atomic/atomic_fixnum_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,5 +234,14 @@ module Concurrent
234234
expect(AtomicFixnum.ancestors).to include(MutexAtomicFixnum)
235235
end
236236
end
237+
238+
describe '#to_s and #inspect' do
239+
it 'includes the value' do
240+
subject = described_class.new(42)
241+
expect(subject.to_s).to include('42')
242+
expect(subject.inspect).to include('42')
243+
end
244+
end
245+
237246
end
238247
end

spec/concurrent/atomic/atomic_reference_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ module Concurrent
147147

148148
describe AtomicReference do
149149
it_should_behave_like :atomic_reference
150+
151+
describe '#to_s and #inspect' do
152+
it 'includes the value' do
153+
subject = described_class.new('kajhsd')
154+
expect(subject.to_s).to include('kajhsd')
155+
expect(subject.inspect).to include('kajhsd')
156+
end
157+
end
150158
end
151159

152160
describe MutexAtomicReference do

0 commit comments

Comments
 (0)