Here-Docs 包含 if then else 语句
正如标题所说。是否可以在 ruby 的此处文档中使用 if/then 语句?
谢谢
Like the title says. Is it possible to use an if/then statement in a here-doc for ruby.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的
Yes
比较(和对比)这些样本。哪个更具可读性,逻辑更清晰,你想支持吗?:
示例#1:
示例#2:
示例#3:
示例#4:
我个人更喜欢#4,因为它将逻辑与字符串的定义分开,有助于更轻松地了解正在发生的情况,但您的情况可能会有所不同。
Compare (and contrast) these samples. Which is more readable, has clearer logic, and would you want to support?:
Example #1:
Example #2:
Example #3:
Example #4:
Personally I prefer #4 because it separates the logic from the definition of the string, helping to keep it easier to see what is going on, but your mileage might vary.
定界符就像一个字符串。因此,如果您的字符串允许变量求值和替换(不使用引号或双引号),您可以执行以下操作:
您必须将条件放在括号内,这是标准的字符串内求值技术。
另请注意,如果您的定界文档的引用使用单引号,这将不起作用,这意味着它是一个文字字符串。
A heredoc is just like a string. So if your string allows variable evaluation and substitution (using no quotes or double quotes), you can do something like this:
You have to put your conditions inside the brackets, standard in-string evaluation technique.
Please also note that this won't work if the quoting for your heredoc uses single quotes, meaning it's a literal string.