Created
November 14, 2018 13:43
-
-
Save r7kamura/dd323deadbdd820d8d293c3ad6187a89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
標準出力: