Skip to content

Single- vs multiline lambdas #479

Open
@bquorning

Description

@bquorning

The style guide currently says

Use the new lambda literal syntax for single line body blocks. Use the lambda method for multi-line blocks. [link]

# bad
l = lambda { |a, b| a + b }
l.call(1, 2)

# correct, but looks extremely awkward
l = ->(a, b) do
  tmp = a * 7
  tmp * b / 50
end

# good
l = ->(a, b) { a + b }
l.call(1, 2)

l = lambda do |a, b|
  tmp = a * 7
  tmp * b / 50
end

I disagree. Having to change lambda syntax from -> {} to lambda do; end when a lambda grows too large for one line seems wrong. For multi-line lambdas, I would prefer using the stabby lambda syntax with a curly-brace block:

# good
l = ->(a, b) {
  tmp = a * 7
  tmp * b / 50
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions