I wonder, is it possible to do something similar in Ruby to what I can do in Scala or other languages:
someCollection.foreach(x => println(x)) // a full version
someCollection.foreach(println) // a short version
some_array.each { |x| puts x }
some_array.each { puts }
some_other_method
some_array.map { some_other_method }
some_array.map(some_other_method) # ???
def some_other_method a
# ... doing something with a
end
If you look up the rules for implicit η-expansion in the SLS (§6.26.5), it should be immediately obvious that it relies crucially on static type information and thus cannot possibly work in Ruby.
You can, however, explicitly obtain a Method
object via reflection. Method
objects respond to to_proc
and like any object that responds to to_proc
can thus be passed as if they were blocks using the unary prefix &
operator:
some_array.each(&method(:puts))