summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormokha <mokha@cisco.com>2017-09-18 10:24:55 -0600
committermokha <mokha@cisco.com>2017-09-18 10:24:55 -0600
commit50f7d9bc8384b209b35a93cf87f168bcf2efebd1 (patch)
tree2bcff808e6dcd3716a24576af3ff7619403df36b
parent0aafd6fc6df1ac91d5bcc8de12ee22cf4cd4c206 (diff)
extract method.
-rw-r--r--spec/heaps_stacks_queues/simplify_path_spec.rb18
1 files changed, 10 insertions, 8 deletions
diff --git a/spec/heaps_stacks_queues/simplify_path_spec.rb b/spec/heaps_stacks_queues/simplify_path_spec.rb
index af40158..6516d91 100644
--- a/spec/heaps_stacks_queues/simplify_path_spec.rb
+++ b/spec/heaps_stacks_queues/simplify_path_spec.rb
@@ -34,19 +34,21 @@ The simplified path.
DOC
describe "simplify_path" do
- def simplify_path(path)
- result = []
- path.split('/').each do |part|
+ def reduce(path)
+ path.split('/').inject([]) do |stack, part|
case part
- when '.'
+ when '.', ''
when '..'
- result.pop
- when ''
+ stack.pop
else
- result.push(part)
+ stack.push(part)
end
+ stack
end
- "/" + result.join('/')
+ end
+
+ def simplify_path(path)
+ "/#{reduce(path).join('/')}"
end
[