0

我想知道是否有人(比如我自己=])可以创建一些方法来做这样的事情:

x = magic_method do
  create_string "some_string" # a method that returns "This is <some string>\n "
  create_string "other_string"
  create_string "third_string"
end

然后会返回

puts x #=> This is some_string\n This is other_string\n This is third_string
4

1 回答 1

0

几个选项。最简单的方法是改变原始字符串:

def create_string(str)
  str.sub!(/\A/, "This is ") << "\n"
end
于 2012-05-15T00:50:08.393 回答