Heads up: This description was created by AI and might not be 100% accurate.
text2array.rb
This Ruby code snippet demonstrates string manipulation. It first defines a multi-line string text
using a heredoc. Then, it splits the string into an array of lines using lines
, and removes trailing whitespace (like newlines) from each line using chomp
, resulting in an array of strings without newline characters.
Ruby code snippet
text = <<~TEXT
a
b
c
TEXT
#=> "a\nb\nc\n"
lines = text.lines.map(&:chomp)
#=> ["a", "b", "c"]
Executed with Ruby 3.4.5
.