Skip to content

Commit 84c57f6

Browse files
authored
Merge pull request #712 from jordan-brough/return-value-from-inside
🌈 Update `Thor::Actions#inside` to return the value yielded by the block
2 parents ce18deb + 3d5e96f commit 84c57f6

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/thor/actions.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ def find_in_source_paths(file)
161161
# to the block you provide. The path is set back to the previous path when
162162
# the method exits.
163163
#
164+
# Returns the value yielded by the block.
165+
#
164166
# ==== Parameters
165167
# dir<String>:: the directory to move to.
166168
# config<Hash>:: give :verbose => true to log and use padding.
@@ -179,16 +181,18 @@ def inside(dir = "", config = {}, &block)
179181
FileUtils.mkdir_p(destination_root)
180182
end
181183

184+
result = nil
182185
if pretend
183186
# In pretend mode, just yield down to the block
184-
block.arity == 1 ? yield(destination_root) : yield
187+
result = block.arity == 1 ? yield(destination_root) : yield
185188
else
186189
require "fileutils"
187-
FileUtils.cd(destination_root) { block.arity == 1 ? yield(destination_root) : yield }
190+
FileUtils.cd(destination_root) { result = block.arity == 1 ? yield(destination_root) : yield }
188191
end
189192

190193
@destination_stack.pop
191194
shell.padding -= 1 if verbose
195+
result
192196
end
193197

194198
# Goes to the root and execute the given block.

spec/actions_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,19 @@ def file
165165
end
166166
end
167167

168+
it "returns the value yielded by the block" do
169+
expect(runner.inside("foo") { 123 }).to eq(123)
170+
end
171+
168172
describe "when pretending" do
169173
it "no directories should be created" do
170174
runner.inside("bar", :pretend => true) {}
171175
expect(File.exist?("bar")).to be false
172176
end
177+
178+
it "returns the value yielded by the block" do
179+
expect(runner.inside("foo") { 123 }).to eq(123)
180+
end
173181
end
174182

175183
describe "when verbose" do

0 commit comments

Comments
 (0)