在 Ruby 中构建长字符串的简洁方法

2024-01-31

在编写 Ruby(客户端脚本)时,我看到了三种构建更长字符串的方法,包括行结束符,所有这些对我来说“闻起来”都有点难看。

有没有更干净、更好的方法?

变量递增。

if render_quote?
  quote =  "Now that there is the Tec-9, a crappy spray gun from South Miami."
  quote += "This gun is advertised as the most popular gun in American crime. Do you believe that shit?"
  quote += "It actually says that in the little book that comes with it: the most popular gun in American crime."
  quote += "Like they're actually proud of that shit."
  puts quote
end

Heredocs(以及未闭合的引用)。

if render_quote?
  quote =<<EOS
Now that there is the Tec-9, a crappy spray gun from South Miami.
This gun is advertised as the most popular gun in American crime. Do you believe that shit?
It actually says that in the little book that comes with it: the most popular gun in American crime.
Like they're actually proud of that shit.
EOS

  puts quote
end

或者,只需不添加结束标签即可:

if render_quote?
  quote = "Now that there is the Tec-9, a crappy spray gun from South Miami.
This gun is advertised as the most popular gun in American crime. Do you believe that shit?
It actually says that in the little book that comes with it: the most popular gun in American crime.
Like they're actually proud of that shit."

  puts quote
end

或者,可选地,用 gsub 来修复缩进 https://stackoverflow.com/questions/3772864/how-do-i-remove-leading-whitespace-chars-from-ruby-heredoc-问题(哎哟!?)。

连接。

if render_quote?
  quote =  "Now that there is the Tec-9, a crappy spray gun from South Miami."
  quote += "This gun is advertised as the most popular gun in American crime. Do you believe that shit?"
  quote += "It actually says that in the little book that comes with it: the most popular gun in American crime."
  quote += "Like they're actually proud of that shit."
  puts quote
end

(引用自塞缪尔·L·伊普苏姆 http://slipsum.com/ )

我知道在我的脚本中使用这样的字符串(即视图逻辑)本身就是一种味道,但不知道有什么模式(除了 po 文件等)可以清理它。


请注意,相邻的字符串文字是连接在一起的。您可以将其与行继续字符结合起来\.

if render_quote?
  quote =
  "Now that there is the Tec-9, a crappy spray gun from South Miami. " \
  "This gun is advertised as the most popular gun in American crime. " \
  "Do you believe that shit?" \
  "It actually says that in the little book that comes with it: " \
  "the most popular gun in American crime. " \
  "Like they're actually proud of that shit."
  puts quote
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Ruby 中构建长字符串的简洁方法 的相关文章

随机推荐

  • PCRE正则表达式非连续重复

    我尝试最少 6 个字符 总共最多 15 个字符 第一个必须是字母数字 无特殊 接下来 最多 13 个字符必须是字母数字 并且可以包含不连续 一次只能包含以下一项 下划线或句点或连字符 最后一个字符必须是字母数字 好的示例 A 3 hj 3J
  • 如何使用 pyspark 读取 Parquet 文件、更改数据类型并写入 Hadoop 中的另一个 Parquet 文件

    我的源镶木地板文件的所有内容都是字符串 我的目标 parquet 文件需要将其转换为不同的数据类型 如 int 字符串 日期等 我该如何执行此操作 您可能希望应用用户定义的架构来加速数据加载 有两种方法可以应用它 使用输入 DDL 格式的字
  • 我们可以使用 1 个表来实现布谷鸟哈希吗?

    我发现关于Cuckoo 哈希表 http en wikipedia org wiki Cuckoo hashing他们看起来不错 但我发现的大多数示例代码都使用 2 个表来实现这一点 在我看来 这是错误的 因为这两个表可能位于不同的内存页面
  • 检查一个 numpy 数组中有多少个 numpy 数组与另一个不同大小的 numpy 数组中的其他 numpy 数组相等

    我的问题 假设我有 a np array np array 1 2 np array 3 4 np array 5 6 np array 7 8 np array 9 10 b np array np array 5 6 np array
  • target=_blank 不适用于 GA 出站链接跟踪

    我想跟踪出站链接的点击并实现以下代码 GA code var trackOutboundLink function url ga send event outbound click url hitCallback function docu
  • 错误 ITMS-90096:您的二进制文件未针对 iPhone 5 进行优化

    我收到此错误 查看 LaunchImage launchimage 文件夹 我有一个 email protected cdn cgi l email protection但不是Default 568h png image 似乎没有空间容纳一
  • DELETE 语句冲突 REFERENCE 约束

    我收到错误 DELETE 语句与 REFERENCE 约束冲突 FK DocYDocument1 表 DocYDocument 列 SubDocID 语句 从文档中删除 ID 参数 D7FAA3CF 表 DocYDocument 有列 PK
  • project.json 中的框架和导入部分:它们是什么?

    到底是什么frameworks and imports的部分project jsonASP NET Core 1 0 项目的文件以及它们的作用是什么 我一直在尝试寻找 官方 文档以更好地理解它 下面是我最近启动的一个 Yeoman 支架 A
  • 在java servlet中处理“多部分/相关”

    在 Jetty 8 下运行的 Servlet 接收以下请求 Header Content Type multipart related boundary example Data example content type text xml
  • Java中如何获取for循环的最后一个值?

    import java util Scanner public class Problem1 public static void main String args input Scanner kb new Scanner System i
  • 访问网站时如何检查自己的会话哈希?

    我有兴趣了解当我访问网站时人们在我的会话和 cookie 中存储了什么 有什么方法可以查看请求之间以及我在 Safari Chrome 或 Firefox 中的页面上时发生的情况吗 在 Firefox 中 您可以使用其他Firebug ht
  • Google Sheets 脚本:按名称引用图表

    我试图在 图表 工作表中创建一个 用户界面 这将允许他们从下拉列表中选择一个图表 然后它将显示在该下拉列表的正下方 最终会有大量图表 可能在另一个 图表数据 表上 所以我的想法是将图表从数据表移动到图表表 来回移动 不确定这是否可以做到 我
  • 有条件调用构造函数[关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 假设某个类foo有两个自定义构造函数 例如foo foo bar const and foo foo baz const 根据某些条件调用其中任
  • 我可以从空 std 容器的 front() 得到什么?

    If front 返回一个引用 并且容器是空的 我会得到什么 未定义的引用 这是否意味着我需要检查empty 在每个之前front 你会得到未定义的行为 在调用 front 之前 你需要使用 empty 检查容器是否为空 检查容器是否包含某
  • 下载已上传的 Lambda 函数

    我使用 upload zip 在 AWS Python 中创建了一个 lambda 函数 我丢失了这些文件 需要进行一些更改 有什么方法可以下载该 zip 吗 Yes 导航到您的 lambda 函数设置 在右上角您将有一个名为 的按钮 Ac
  • 与模板类交朋友:编译错误

    我试图使用指向实现惯用语的指针来隐藏我正在使用 Concurrency unbounded buffer 来自 VC 2010 的事实 问题是 我正在使用模板执行此操作 但陷入了编译错误 这是代码 阻塞队列 h pragma once na
  • 如何在 php 中制作访客计数器? [关闭]

    这个问题不太可能对任何未来的访客有帮助 它只与一个较小的地理区域 一个特定的时间点或一个非常狭窄的情况相关 通常不适用于全世界的互联网受众 为了帮助使这个问题更广泛地适用 访问帮助中心 help reopen questions 我如何计算
  • 如何多次使用ConsoleCancelEventHandler

    我一直忙于编写一个充当前端的应用程序 它有一个 GUI 可以使用按钮和类似的东西来获取命令行选项 并将它们传递给命令行 exe 它使用应用程序的控制台来显示命令行应用程序的输出 这工作正常 但是当使用 Ctrl C 或尝试关闭控制台窗口时
  • 安装 Realm 后出错:必须首先使用有效主机创建 RPC 会话

    我最近使用以下方式安装了领域 npm i realm S react native link realm 重要编辑仅当从设备调试时才会出现此问题 一切仍然有效 但这一次 当我尝试启用Debug Js Remotely从开发菜单中 我收到错误
  • 在 Ruby 中构建长字符串的简洁方法

    在编写 Ruby 客户端脚本 时 我看到了三种构建更长字符串的方法 包括行结束符 所有这些对我来说 闻起来 都有点难看 有没有更干净 更好的方法 变量递增 if render quote quote Now that there is th