End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
String#lines
str.lines(separator=$/, chomp: false) → an_array
string.c
View on GitHub ↗
static VALUE
rb_str_lines(int argc, VALUE *argv, VALUE str)
{
VALUE ary = WANTARRAY("lines", 0);
return rb_str_enumerate_lines(argc, argv, str, ary);
}
Returns an array of lines in str split using the supplied record separator ($/ by default). This is a shorthand for str.each_line(separator, getline_args).to_a.
If chomp is true, separator will be removed from the end of each line.
Example
"hello\nworld\n".lines #=> ["hello\n", "world\n"] "hello world".lines(' ') #=> ["hello ", " ", "world"] "hello\nworld\n".lines(chomp: true) #=> ["hello", "world"]
If a block is given, which is a deprecated form, works the same as each_line.