Answer the question
In order to leave comments, you need to log in
How to understand the construction of lambda and ampersand?
there is such a code
decorator = -> (item) do
# действия с item
end
[array].select(& ->(item) { item[:auth] }).map(&decorator)
[array].select(& ->(item) { item[:auth] }).map(&decorator)
[array].select{|item| item[:auth] }.map(&decoration)
& ->(item) { item[:auth] }
Answer the question
In order to leave comments, you need to log in
decorator is an object of type Proc (lambda, but not the gist). An ampersand before an object of type Proc turns it into a block, which is NOT an object and cannot be instantiated in any way. If an ampersand is in front of an object that is NOT of type Proc, then it first tries to call on it a method called "to_proc", which specifies the logic for turning the object into an object of type Proc, which in turn will be converted to block (example - Symbol, map(&:to_i))
"#select" and "#map" expect block as input
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question