4 ms·
About the strange Ruby pipes when iterating thing... Ruby, like many languages, has support for anonymous functions (lambdas, procs, blocks, etc.) The pipes ar
by planckscnst 13y ago
About the strange Ruby pipes when iterating thing...
Ruby, like many languages, has support for anonymous functions (lambdas, procs, blocks, etc.) The pipes are the delimiters for the formal parameters that the function receives. Most languages use parentheses for that. For instance, you might expect something that looks like this:
function_that_accepts_a_lambda(arg1, arg2, lambda(lambda_arg1, lambda_arg2) { lambda body; })
Here, a function body is defined and passed as an argument in the same place other arguments are listed. Also, the parameters of that function are defined outside the body of the function, within parentheses.
In Ruby, the format is just different. You can bind a lambda to a variable and pass that variable in just like any other argument, but if you want to give an actual unbound function body as an argument, you have to use the Ruby block syntax, which delimits the block using either `do`...`end` or `{`...`}`, and immediately after the beginning delimiter, you can define formal parameters. The definition of the formal parameters of the block is delimited by pipe `|` characters. The other strange part is that the block is not presented as an argument quite the same as the others: it is outside the list of formal parameters for the function receiving the block. The previous example in Ruby is
function_that_accepts_a_lambda(arg1, arg2) { |lambda_arg1, lambda_arg2| lambda body }
The name of the function now is slightly misleading since in Ruby, every function can receive a block (although it may not do anything with it).
I can assure you that after working with the language for a while, that syntax is not a problem at all. I had, in fact, completely forgotten that I thought it was strange when I first learned Ruby. There are things much more annoying in Ruby than that syntax, like an abundance of slightly different ways to define a closure, each with subtly different effects.[1] Overall, however, it is quite a nice language to work with.
[1] http://innig.net/software/ruby/closures-in-rubyhttp://innig.net/software/ruby/closures-in-ruby http://innig.net/software/ruby/closures-in-rubyhttp://innig....
- mattkrea 13y agoWow. Thank you very much for that. It is somewhat of a jarring change from what I'm use to but I at least understand it now.