christian christian - 1 year ago 103
Ruby Question

Ruby Curly Brackets for "set" value

What's the difference between these two variations in Ruby

set: example1, "/random/string"


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

Do they both have the same effect?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download