红宝石 |字符串 each_line 方法
each_line是 Ruby 中的 String 类方法,用于拆分给定的字符串,将提供的参数作为记录分隔符(默认为 $/),依次将每个子字符串传递给提供的块。如果提供了零长度的记录分隔符,则字符串将分成由多个连续换行符分隔的段落,
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.
示例 1:
# Ruby program to demonstrate
# the each_line method
# Taking a string and
# using the method
puts "Ruby\nString".each_line {|s| p s}
输出:
"Ruby\n"
"String"
Ruby
String
示例 2:
# Ruby program to demonstrate
# the each_line method
# Taking a string and
# using the method
puts "Sample\nInput".each_line {|s| p s}
输出:
"Sample\n"
"Input"
Sample
Input