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 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 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