Converting stabby lambda to lambda keyword results in ArgumentError
See original GitHub issueMetadata
- 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:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Ehh wasn’t so hard. Going to merge the fix, will be part of the next release.
Yeah the problem here is operator precedence. The lambda should be using
{}
(which has higher precedence) versusdo...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.