question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Converting stabby lambda to lambda keyword results in ArgumentError

See original GitHub issue

Metadata

  • Ruby version: ruby 2.6.2p47 (2019-03-13 revision 67232) [x86_64-darwin18]
  • @prettier/plugin-ruby version: 0.12.0

Input

class ReadingResource < ApplicationResource
  filter :complete, apply: ->(records, value, _options) {
    if value == ['true']
      records.where('readings.completed_at IS NOT NULL')
    else
      records.where('readings.completed_at IS NULL')
    end
  }
end

Current output

class ReadingResource < ApplicationResource
  filter :complete, apply: lambda do |records, value, _options|
    if value == %w[true]
      records.where('readings.completed_at IS NOT NULL')
    else
      records.where('readings.completed_at IS NULL')
    end
  end
end

This output results in an error when executing:

ArgumentError:
  tried to create Proc object without a block

I’m not quite sure why this is happening; it sounds like a difference of precedence between the lambda keyword and stabby lambdas, like the block is being passed to #filter instead of to lambda. But I can’t find documentation on a precedence difference.

In case it helps, if I provide parents for #filter the error is fixed:

class ReadingResource < ApplicationResource
  filter(
    :complete,
    apply:
      lambda do |records, value, _options|
        if value == %w[true]
          records.where('readings.completed_at IS NOT NULL')
        else
          records.where('readings.completed_at IS NULL')
        end
      end,
  )
end

Expected output

I don’t have a preference between the lambda keyword and stabby lambdas, but if there’s a syntactic reason the lambda keyword can’t be used here then I think it’d be best to keep the stabby lambda:

class ReadingResource < ApplicationResource
  filter :complete, apply: ->(records, value, _options) {
    if value == ['true']
      records.where('readings.completed_at IS NOT NULL')
    else
      records.where('readings.completed_at IS NULL')
    end
  }
end

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kddnewtoncommented, Apr 21, 2019

Ehh wasn’t so hard. Going to merge the fix, will be part of the next release.

1reaction
kddnewtoncommented, Apr 21, 2019

Yeah the problem here is operator precedence. The lambda should be using {} (which has higher precedence) versus do...end. I don’t think I’ve seen the case where it’s a multi-line breaking lambda as a hash value, which is probably the problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ruby: Can lambda function parameters have default values?
In Ruby 1.9.3 I get a syntax error when putting a space between the stabby lamba and the parenthesis f -> (x =...
Read more >
Using Lambdas in Ruby - Honeybadger Developer Blog
Stabby Lambdas. In this article I've used the lambda keyword for clarity. But there's a more concise syntax for defining lambdas introduced in ......
Read more >
Understanding Ruby - Blocks, Procs, and Lambdas
Note: This syntax, for reference, is known as "stabby lambda". The arguments are to the right of the arrow and the body of...
Read more >
Understanding Ruby Blocks | Dennis O'Keeffe Blog
Using the lambda keyword. Using the -> syntax. · If we use the lambda keyword, we can pass arguments to the block using...
Read more >
Weird Ruby Part 4: Code Pods (Blocks, Procs, and Lambdas)
All of the methods that generate procs and lambdas use this function: proc_new . When you want your proc to have lambda behavior...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found