在 Ruby 中以编程方式构建多行字符串

发布于 2024-08-07 05:05:55 字数 461 浏览 5 评论 0原文

这是我在编程时经常做的事情:

code = ''
code << "next line of code #{something}" << "\n"
code << "another line #{some_included_expression}" << "\n"

有没有比 << 更好的方法?每行都使用“\n”+“\n” 这看起来效率很低。

我对 Ruby 解决方案特别感兴趣。我在想类似的事情

code = string.multiline do
  "next line of code #{something}"
  "another line #{some_included_expression}"
end

Here's something I often do when programming:

code = ''
code << "next line of code #{something}" << "\n"
code << "another line #{some_included_expression}" << "\n"

Is there some better way than having << "\n" or + "\n" on every line? This seems quite inefficient.

I'm interested in Ruby solutions, in particular. I'm thinking something like

code = string.multiline do
  "next line of code #{something}"
  "another line #{some_included_expression}"
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

給妳壹絲溫柔 2024-08-14 05:05:55

如果您想要构建文本块,最简单的方法就是使用 % 运算符。例如:

code = %{First line
second line
Third line #{2 + 2}}

“代码”将是

"First line\n second line\n Third line 4"

If you're looking to build a block of text, the easy way to do it is to just use the % operator. For example:

code = %{First line
second line
Third line #{2 + 2}}

'code' will then be

"First line\n second line\n Third line 4"
岁吢 2024-08-14 05:05:55

这是一种方式:

code = []
code << "next line of code #{something}"
code << "another line #{some_included_expression}"
code.join("\n")

This would be one way:

code = []
code << "next line of code #{something}"
code << "another line #{some_included_expression}"
code.join("\n")
走过海棠暮 2024-08-14 05:05:55

使用 <<- 运算符:

code = <<-CODE
var1 = "foo"
var2 = "bar"
CODE

Use <<- operator:

code = <<-CODE
var1 = "foo"
var2 = "bar"
CODE
冷默言语 2024-08-14 05:05:55

我想,您只需在字符串中嵌入 ...\n" 就可以了。这是一种有趣的方法:

class String
  def / s
    self << s << "\n"
  end
end

然后

f = ""           # => ""
f / 'line one'   # => "line one\n"
f / 'line two'   # => "line one\nline two\n"
f / 'line three' # => "line one\nline two\nline three\n"

这将启用类似的功能:

"" / "line 1" / "line 2" / "line 3" # => "line 1\nline 2\nline 3\n"

或者甚至:

f/
"line one"/
"line two"/
"line three"     # => "line one\nline two\nline three\n"

It would work for you to just embed ...\n" in your strings, I suppose. Here is a fun way to do it:

class String
  def / s
    self << s << "\n"
  end
end

then

f = ""           # => ""
f / 'line one'   # => "line one\n"
f / 'line two'   # => "line one\nline two\n"
f / 'line three' # => "line one\nline two\nline three\n"

This would enable something like:

"" / "line 1" / "line 2" / "line 3" # => "line 1\nline 2\nline 3\n"

Or even:

f/
"line one"/
"line two"/
"line three"     # => "line one\nline two\nline three\n"
三生路 2024-08-14 05:05:55

这是此处介绍的方法:

str = <<end.margin
  |This here-document has a "left margin"
  |at the vertical bar on each line.
  |
  |  We can do inset quotations,
  |  hanging indentions, and so on.
end

这是通过使用这个:

class String
  def margin
    arr = self.split("\n")             # Split into lines
    arr.map! {|x| x.sub!(/\s*\|/,"")}  # Remove leading characters
    str = arr.join("\n")               # Rejoin into a single line
    self.replace(str)                  # Replace contents of string
  end
end

我想这个问题是:缺乏可移植性/猴子补丁的存在是否会使这个解决方案变得糟糕。

Here's a method presented here:

str = <<end.margin
  |This here-document has a "left margin"
  |at the vertical bar on each line.
  |
  |  We can do inset quotations,
  |  hanging indentions, and so on.
end

This is accomplished by using this:

class String
  def margin
    arr = self.split("\n")             # Split into lines
    arr.map! {|x| x.sub!(/\s*\|/,"")}  # Remove leading characters
    str = arr.join("\n")               # Rejoin into a single line
    self.replace(str)                  # Replace contents of string
  end
end

I guess the question with this is: does the lack of portability / presence of monkey patching make this solution bad.

无声无音无过去 2024-08-14 05:05:55

有什么问题:

code = "next line of code #{something}\n"+
       "another line #{some_included_expression}"

What's wrong with:

code = "next line of code #{something}\n"+
       "another line #{some_included_expression}"
丶情人眼里出诗心の 2024-08-14 05:05:55

您可以将多行文本放入一个文件中,并使用 ERB 来解析它(注意 ERB 包含在 Ruby 中)

require 'erb'

multi_line_string = File.open("multi_line_string.erb", 'r').read
template = ERB.new(multi_line_string)
template.result(binding)

(ERB 可以从 Binding 访问变量,Binding 是一个对象,提供对实例方法和变量的访问,这些实例方法和变量属于通过将其设置为“绑定”,它指向自身)

文档 这里

You could place your multi-line text in a file, and use ERB to parse it (note ERB is included with Ruby)

require 'erb'

multi_line_string = File.open("multi_line_string.erb", 'r').read
template = ERB.new(multi_line_string)
template.result(binding)

(ERB can access variables from a Binding, an object that provides access to the instance methods and variables that are owned by another object. By setting it to "binding" it points to itself)

Documentation here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文