kapso
kapso

Reputation: 11903

Rails cache curly braces block syntax error

Anyone know why I get a syntax error when I try to use curly braces block syntax for Rails.cache.fetch

This errors out with error (syntax error, unexpected '{', expecting keyword_end (SyntaxError) )

Rails.cache.fetch "person/#{id}" { find(id) }

The following works:

Rails.cache.fetch "person/#{id}" do
  find(id)
end

Upvotes: 0

Views: 207

Answers (2)

davidrac
davidrac

Reputation: 10738

This is because {} has different precedence than do end. In the first expression they are tied to the "person/#{id}" part of the expression.

So your first example is similar to:

Rails.cache.fetch("person/#{id}" { find(id) })

while the second is:

Rails.cache.fetch("person/#{id}") { find(id) }

Upvotes: 0

pkurek
pkurek

Reputation: 606

the short block needs () before to recognise the syntax it should look something like

Rails.cache.fetch("person/#{id}") { find(id) }

Upvotes: 0

Related Questions