Ruby | String each_line Method
Last Updated :
12 Dec, 2019
each_line is a String class method in Ruby which is used to split the given string sing the supplied parameter as the record separator ($/ by default), passing each substring in turn to the supplied block. The string is split into paragraphs delimited by multiple successive newlines if a zero-length record separator is supplied,
Syntax: str.each_line(separator=$/ [, getline_args])
Parameters: Here, str is the given string.
Returns: An enumerator if the no block is given. Otherwise the splitted string.
Example 1:
puts "Ruby\nString" .each_line {|s| p s}
|
Output:
"Ruby\n"
"String"
Ruby
String
Example 2:
puts "Sample\nInput" .each_line {|s| p s}
|
Output:
"Sample\n"
"Input"
Sample
Input
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...