如何传递多行String参数?

时间:2013-11-02 10:03:18

标签: ruby string

我们说我有这种方法:

def read_line_by_line(some_text)
  some_text.each |line| do (something) end
end

我该怎么做?我有:

my first line
of the input text

我尝试将其作为参数传递,我得到了一个奇怪的输出。它没有逐行阅读。

1 个答案:

答案 0 :(得分:0)

这是你在尝试的事情:

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