The following two scopes generate the same result, which syntax is preferable and is there any other difference?
scope :paid, lambda { |state| where(state: state) }
scope :paid, ->(state) { where(state: state) }
You can run Ruby code in AWS Lambda. Lambda provides runtimes for Ruby that run your code to process events. Your code runs in an environment that includes the AWS SDK for Ruby, with credentials from an AWS Identity and Access Management (IAM) role that you manage.
In Ruby, a lambda is an object similar to a proc. Unlike a proc, a lambda requires a specific number of arguments passed to it, and it return s to its calling method rather than returning immediately. proc_demo = Proc. new { return "Only I print!" }
When using parameters prefixed with ampersands, passing a block to a method results in a proc in the method's context. Procs behave like blocks, but they can be stored in a variable. Lambdas are procs that behave like methods, meaning they enforce arity and return as methods instead of in their parent scope.
Working with Lambdas in Ruby my_lambda_function = lambda { puts "Hello, Geeks !" } We have different ways to call this function. We can use my_lambda. call, my_lambda.
It's preferable, due to readibility reasons, to use new syntax ->
(introduced in Ruby 1.9) for single-line blocks and lambda
for multi-line blocks. Example:
# single-line
l = ->(a, b) { a + b }
l.call(1, 2)
# multi-line
l = lambda do |a, b|
tmp = a * 3
tmp * b / 2
end
l.call(1, 2)
It seems a community convention established in bbatsov/ruby-style-guide.
So, in your case, would be better:
scope :paid, ->(state) { where(state: state) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With