Skip to content

Instantly share code, notes, and snippets.

@r7kamura
Created November 14, 2018 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r7kamura/dd323deadbdd820d8d293c3ad6187a89 to your computer and use it in GitHub Desktop.
Save r7kamura/dd323deadbdd820d8d293c3ad6187a89 to your computer and use it in GitHub Desktop.
text = <<EOS
.a
.a
-a
+a
+a
.a
-a
-a
+a
+a
.a
.a
-a
+a
-a
.a
EOS
# 分割
#
# . は続く . と結合する
# - は続く - と結合する
# - は続く + と結合する
# + は続く + と結合する
chunks = text.each_line.chunk_while do |a, b|
a.start_with?('.') && b.start_with?('.') ||
a.start_with?('-') && b.start_with?('-') ||
a.start_with?('-') && b.start_with?('+') ||
a.start_with?('+') && b.start_with?('+')
end
# 除去
chunks = chunks.reject do |chunk|
chunk.first.start_with?(' ')
end
# 整形
require 'json'
puts JSON.pretty_generate(chunks.to_a)
@r7kamura
Copy link
Author

標準出力:

[
  [
    ".a\n",
    ".a\n"
  ],
  [
    "-a\n",
    "+a\n",
    "+a\n"
  ],
  [
    ".a\n"
  ],
  [
    "-a\n",
    "-a\n",
    "+a\n",
    "+a\n"
  ],
  [
    ".a\n",
    ".a\n"
  ],
  [
    "-a\n",
    "+a\n"
  ],
  [
    "-a\n"
  ],
  [
    ".a\n"
  ]
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment