christian christian - 3 months ago 22
Ruby Question

Ruby Curly Brackets for "set" value

What's the difference between these two variations in Ruby

set: example1, "/random/string"


and

set: example2, -> {"random/string"}


Do they both have the same effect?

Answer

Although they look superficially similar they're actually two completely different things.

The first is a simple string, the second is a Proc which returns a string. Many methods in the Ruby world take both, the Proc version is a way of deferring evaluation of something until if and when it's needed.

The -> { ... } notation is shorthand for lambda { ... }, it's called the stabby-lambda operator.