gpt4 book ai didi

ruby - 如何使用 HEREDOC 作为参数传递给方法?

转载 作者:行者123 更新时间:2023-12-05 08:45:11 27 4
gpt4 key购买 nike

代码示例:

create_data_with(
first: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
second: <<~TEXT
Aenean vel ex bibendum, egestas tortor sit amet, tempus lorem. Ut sit
amet rhoncus eros. Vestibulum ante ipsum primis in faucibus orci
luctus et ultrices posuere cubilia curae; Quisque non risus vel lacus
tristique laoreet. Curabitur quis auctor mauris, nec tempus mauris.
TEXT,
third: "Nunc aliquet ipsum at semper sodales."
)

错误出现在这一行:

second: <<~TEXT

RuboCop 是这样描述的:

Lint/Syntax: unterminated string meets end of file
(Using Ruby 3.1 parser; configure using TargetRubyVersion parameter, under AllCops)
second: <<~TEXT

你能告诉我语法应该是什么吗?我需要保留 <<~ 的外观和用途.

最佳答案

另一种选择是在方法调用之后移动 heredoc。但是,由于 heredoc 从其标识符之后的行开始,因此您的方法调用不得跨越多行:

create_data_with(first: "foo", second: <<~TEXT, third: "bar")
Aenean vel ex bibendum, egestas tortor sit amet, tempus lorem. Ut sit
amet rhoncus eros. Vestibulum ante ipsum primis in faucibus orci
luctus et ultrices posuere cubilia curae; Quisque non risus vel lacus
tristique laoreet. Curabitur quis auctor mauris, nec tempus mauris.
TEXT

对于更长的值,您可以使用多个 heredocs:

create_data_with(first: <<~FIRST, second: <<~SECOND, third: <<~THIRD)
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
FIRST
Aenean vel ex bibendum, egestas tortor sit amet, tempus lorem. Ut sit
amet rhoncus eros. Vestibulum ante ipsum primis in faucibus orci
luctus et ultrices posuere cubilia curae; Quisque non risus vel lacus
tristique laoreet. Curabitur quis auctor mauris, nec tempus mauris.
SECOND
Nunc aliquet ipsum at semper sodales.
THIRD

关于ruby - 如何使用 HEREDOC 作为参数传递给方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73700894/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com