假设我有这个方法:
def read_line_by_line(some_text)
some_text.each |line| do (something) end
end
我怎样才能做到这一点?我有:
my first line
of the input text
我试图将它作为参数传递,我得到了一个奇怪的输出。它不会逐行读取。
这是你正在尝试的:
def read_line_by_line(some_text)
some_text.each_line {|line| puts line }
end
str = <<-eot
my first line
of the input text
eot
read_line_by_line(str)
# >> my first line
# >> of the input text
请参阅 的文档String#each_line
。
更新
def read_line_by_line(some_text)
some_text.each_line {|line| puts line }
end
str = "my first line\nof the input text"
read_line_by_line(str)
# >> my first line
# >> of the input text
对于创建多行字符串,Ruby 支持Here documents
.