LoginSignup
1
0

More than 5 years have passed since last update.

ディレクトリのpathを文字列として処理する

Last updated at Posted at 2016-11-18

0. やりたいこと

例えば/fizz/buzz/hip/hopというpathだったとすると、

/fizz
/fizz/buzz
/fizz/buzz/hip
/fizz/buzz/hip/hop

こういう風にしたい。

要するに

['a', 'b', 'c', 'd']という配列を加工して['a','ab','abc','abcd']という配列を得たい。

1. とりあえずできたけど他にもやり方があるような気がする

target = '/fizz/buzz/hip/hop'
tmparr = target.split("/").delete_if(&:empty?)
dir_array = tmparr.map.with_index { |x,i| (0..i).map {|t| ['/',tmparr[t]].join }.join }
p dir_array
#=> ['/fizz','/fizz/buzz','/fizz/buzz/hip','/fizz/buzz/hip/hop']
  • map.with_indexを利用

時間がかかったくせに今作ってるモジュールで結局この処理は使わなかった。備忘のため保存。

1
0
10

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0