Skip to content

Commit 65ee9f6

Browse files
committed
Renamed IVar #set? method to #try_set to be more idiomatic.
1 parent 14d4598 commit 65ee9f6

File tree

5 files changed

+40
-38
lines changed

5 files changed

+40
-38
lines changed

lib/concurrent/channel/buffered_channel.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def select(probe)
4040
@probe_set.put(probe)
4141
true
4242
else
43-
shift_buffer if probe.set?([peek_buffer, self])
43+
shift_buffer if probe.try_set([peek_buffer, self])
4444
end
4545

4646
end
@@ -76,7 +76,7 @@ def set_probe_or_push_into_buffer(value)
7676
push_into_buffer(value)
7777
true
7878
else
79-
@probe_set.take.set?([value, self])
79+
@probe_set.take.try_set([value, self])
8080
end
8181
end
8282
end

lib/concurrent/channel/unbuffered_channel.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def probe_set_size
1212
end
1313

1414
def push(value)
15-
until @probe_set.take.set?([value, self])
15+
until @probe_set.take.try_set([value, self])
1616
end
1717
end
1818

lib/concurrent/ivar.rb

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,17 @@ def add_observer(observer = nil, func = :update, &block)
9898
observer
9999
end
100100

101-
# Set the `IVar` to a value and wake or notify all threads waiting on it.
102-
#
103-
# @!macro [attach] ivar_set_parameters_and_exceptions
104-
# @param [Object] value the value to store in the `IVar`
105-
# @yield A block operation to use for setting the value
106-
# @raise [ArgumentError] if both a value and a block are given
107-
# @raise [Concurrent::MultipleAssignmentError] if the `IVar` has already
108-
# been set or otherwise completed
109-
#
110-
# @return [IVar] self
101+
# @!macro [attach] ivar_set_method
102+
# Set the `IVar` to a value and wake or notify all threads waiting on it.
103+
#
104+
# @!macro [attach] ivar_set_parameters_and_exceptions
105+
# @param [Object] value the value to store in the `IVar`
106+
# @yield A block operation to use for setting the value
107+
# @raise [ArgumentError] if both a value and a block are given
108+
# @raise [Concurrent::MultipleAssignmentError] if the `IVar` has already
109+
# been set or otherwise completed
110+
#
111+
# @return [IVar] self
111112
def set(value = NO_VALUE)
112113
check_for_block_or_value!(block_given?, value)
113114
raise MultipleAssignmentError unless compare_and_set_state(:processing, :pending)
@@ -120,12 +121,13 @@ def set(value = NO_VALUE)
120121
end
121122
end
122123

123-
# Set the `IVar` to failed due to some error and wake or notify all threads waiting on it.
124-
#
125-
# @param [Object] reason for the failure
126-
# @raise [Concurrent::MultipleAssignmentError] if the `IVar` has already
127-
# been set or otherwise completed
128-
# @return [IVar] self
124+
# @!macro [attach] ivar_fail_method
125+
# Set the `IVar` to failed due to some error and wake or notify all threads waiting on it.
126+
#
127+
# @param [Object] reason for the failure
128+
# @raise [Concurrent::MultipleAssignmentError] if the `IVar` has already
129+
# been set or otherwise completed
130+
# @return [IVar] self
129131
def fail(reason = StandardError.new)
130132
complete(false, nil, reason)
131133
end
@@ -136,7 +138,7 @@ def fail(reason = StandardError.new)
136138
# @!macro ivar_set_parameters_and_exceptions
137139
#
138140
# @return [Boolean] true if the value was set else false
139-
def set?(value = NO_VALUE, &block)
141+
def try_set(value = NO_VALUE, &block)
140142
set(value, &block)
141143
true
142144
rescue MultipleAssignmentError

spec/concurrent/channel/probe_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,41 @@ def trigger_observable(observable)
2020
it_should_behave_like :observable
2121
end
2222

23-
describe '#set?' do
23+
describe '#try_set' do
2424
context 'empty probe' do
2525
it 'assigns the value' do
26-
probe.set?([32, channel])
26+
probe.try_set([32, channel])
2727
expect(probe.value.first).to eq 32
2828
end
2929

3030
it 'assign the channel' do
31-
probe.set?([32, channel])
31+
probe.try_set([32, channel])
3232
expect(probe.value.last).to be channel
3333
end
3434

3535
it 'returns true' do
36-
expect(probe.set?(['hi', channel])).to eq true
36+
expect(probe.try_set(['hi', channel])).to eq true
3737
end
3838
end
3939

4040
context 'fulfilled probe' do
4141
before(:each) { probe.set([27, nil]) }
4242

4343
it 'does not assign the value' do
44-
probe.set?([88, channel])
44+
probe.try_set([88, channel])
4545
expect(probe.value.first).to eq 27
4646
end
4747

4848
it 'returns false' do
49-
expect(probe.set?(['hello', channel])).to eq false
49+
expect(probe.try_set(['hello', channel])).to eq false
5050
end
5151
end
5252

5353
context 'rejected probe' do
5454
before(:each) { probe.fail }
5555

5656
it 'does not assign the value' do
57-
probe.set?([88, channel])
57+
probe.try_set([88, channel])
5858
expect(probe).to be_rejected
5959
end
6060

@@ -63,7 +63,7 @@ def trigger_observable(observable)
6363
end
6464

6565
it 'returns false' do
66-
expect(probe.set?(['hello', channel])).to eq false
66+
expect(probe.try_set(['hello', channel])).to eq false
6767
end
6868
end
6969
end

spec/concurrent/ivar_shared.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,22 @@
9494
end
9595
end
9696

97-
describe '#set?' do
97+
describe '#try_set' do
9898

9999
context 'when unset' do
100100

101101
it 'assigns the value' do
102-
subject.set?(32)
102+
subject.try_set(32)
103103
expect(subject.value).to eq 32
104104
end
105105

106106
it 'assigns the block result' do
107-
subject.set?{ 32 }
107+
subject.try_set{ 32 }
108108
expect(subject.value).to eq 32
109109
end
110110

111111
it 'returns true' do
112-
expect(subject.set?('hi')).to eq true
112+
expect(subject.try_set('hi')).to eq true
113113
end
114114
end
115115

@@ -118,17 +118,17 @@
118118
before(:each) { subject.set(27) }
119119

120120
it 'does not assign the value' do
121-
subject.set?(88)
121+
subject.try_set(88)
122122
expect(subject.value).to eq 27
123123
end
124124

125125
it 'does not assign the block result' do
126-
subject.set?{ 88 }
126+
subject.try_set{ 88 }
127127
expect(subject.value).to eq 27
128128
end
129129

130130
it 'returns false' do
131-
expect(subject.set?('hello')).to eq false
131+
expect(subject.try_set('hello')).to eq false
132132
end
133133
end
134134

@@ -137,12 +137,12 @@
137137
before(:each) { subject.fail }
138138

139139
it 'does not assign the value' do
140-
subject.set?(88)
140+
subject.try_set(88)
141141
expect(subject).to be_rejected
142142
end
143143

144144
it 'does not assign the block result' do
145-
subject.set?{ 88 }
145+
subject.try_set{ 88 }
146146
expect(subject).to be_rejected
147147
end
148148

@@ -151,7 +151,7 @@
151151
end
152152

153153
it 'returns false' do
154-
expect(subject.set?('hello')).to eq false
154+
expect(subject.try_set('hello')).to eq false
155155
end
156156
end
157157
end

0 commit comments

Comments
 (0)