Open In App

Ruby | String each_line Method

Last Updated : 12 Dec, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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: Ruby
# Ruby program to demonstrate 
# the each_line method 
     
# Taking a string and 
# using the method
puts "Ruby\nString".each_line {|s| p s}
Output:
"Ruby\n"
"String"
Ruby
String
Example 2: Ruby
# Ruby program to demonstrate 
# the each_line method 
     
# Taking a string and 
# using the method
puts "Sample\nInput".each_line {|s| p s}
Output:
"Sample\n"
"Input"
Sample
Input

Next Article

Similar Reads