Ruby 使用heredoc作为哈希值
我有一个接受散列参数的方法Ruby 使用heredoc作为哈希值,ruby,Ruby,我有一个接受散列参数的方法Embed.toggler。在下面的代码中,我试图在散列中使用一个herdeoc Embed.toggler({ title: <<-RUBY #{entry['time']} #{entry['group']['who'] #{entry['
Embed.toggler
。在下面的代码中,我试图在散列中使用一个herdeoc
Embed.toggler({
title: <<-RUBY
#{entry['time']}
#{entry['group']['who']
#{entry['name']}
RUBY
content: content
})
Embed.toggler({
标题:在你的问题是什么之后加一个逗号?@sawa我如何使给定的代码工作并防止错误发生。@sawa嗯,我注意到发布的代码中有语法错误,并认为这就是问题:)我也注意到语法错误,但不知道问题是什么:)+1来自我。这在一般情况下是有效的。但我不知道为什么它在我的代码中不起作用。你知道有没有方法对字符串执行操作吗?例如{title:@AlexeyShein抱歉,我本应该指定我尝试了你的修复,但在运行我的应用程序时错误仍然出现。我打开irb并尝试在哈希值中使用HEREDOC(使用你的建议),结果成功了。所以我接受了你的答案,但仍然不确定为什么我的应用程序中没有使用它。@maxpleaner似乎你也错过了}
在#{entry['group']['who']
中。
syntax error, unexpected ':', expecting tSTRING_DEND
content: content
^
can't find string "RUBY" anywhere before EOF
syntax error, unexpected end-of-input, expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END
title: <<-RUBY
^
Embed.toggler({
title: <<-RUBY,
#{entry['time']}
#{entry['group']['who']
#{entry['name']}
RUBY
content: content
})
title = <<-RUBY
#{entry['time']}
#{entry['group']['who']
#{entry['name']}
RUBY
Embed.toggler(title: title.upcase, content: content)
Embed.toggler({
title: <<-RUBY.upcase,
#{entry['time']}
#{entry['group']['who']
#{entry['name']}
RUBY
content: content
})