Using heredoc as a hash value

I have a method Embed.togglerthat takes a hash argument. With the following code, I am trying to use heredoc in a hash.

                Embed.toggler({
                    title: <<-RUBY 
                        #{entry['time']}
                        #{entry['group']['who']
                        #{entry['name']}
                    RUBY
                    content: content
                })

However, I get the following error trace:

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 
                                      ^

How can I avoid getting this error?

+4
source share
1 answer

Add a comma after <<-RUBY:

Embed.toggler({
    title: <<-RUBY,
        #{entry['time']}
        #{entry['group']['who']
        #{entry['name']}
    RUBY
    content: content
})

it generally works. I am not sure why it did not work in my code.

This did not work, because hashes require that the key / value pair be separated by a comma, for example {title: 'my title', content: 'my content' }, and your code simply does not have a comma. It was hard to understand because of the cumbersome syntax of HEREDOC.

Do you know if there is a way to perform operations on a string?

. ( ) :

title = <<-RUBY
   #{entry['time']}
   #{entry['group']['who']
   #{entry['name']}
RUBY

Embed.toggler(title: title.upcase, content: content)

, , HEREDOC-, , :

Embed.toggler({
    title: <<-RUBY.upcase,
        #{entry['time']}
        #{entry['group']['who']
        #{entry['name']}
    RUBY
    content: content
})

, .

+15

Source: https://habr.com/ru/post/1610208/


All Articles