On Mon, Mar 24, 2003 at 06:17:49AM +0900, Paul Prescod wrote:
> The nice thing about blocks (compared to straightforward higher order 
> functions) is that they have a nice syntax. But I think that they are 
> more limited in that you can pass many higher order functions to a 
> procedure but you can only pass one block (unless you wrap it in a 
> function!). I think.
> 
> e.g.
> 
> def upload(handle_data_func, output_func, print_error_func, block_limit)
>     for blocknum in 0, block_limit
>         try:
>             block = read_block_from_network()
>             output_func("block received " + blocknum)
>             handle_data_func(block)
>         except:
>             print_error_func("something screwed up")
> 
> upload(handle_data_func = lambda data: blocks.append(data),
>        output_func = lambda data: logger.write_msg(data),
>        print_error_func = lambda: logger.write_err(data),
>        block_limit = 5)

You can write that almost exactly the same in Ruby of course: substitute
proc { |foo| ... } for lambda foo:

But Ruby gives you other choices. You could pass a method of an existing
object, e.g. $stdout.method(:write) and $stderr.method(:write)

If I were writing the above, rather than passing in two functions, I'd
probably just pass in two IO-like objects, and call output_obj.write and
error_obj.write respectively. Ruby's convention that things you can write to
have a method called 'write' is pretty simple, and you can always add a
'write' object to something which doesn't already have one.

I skipped over a lot of this discussion, but did anyone mention space-
sensitive syntax? It's so nice to be able to put

  if false
  ...
  end

around a piece of code, and not have to re-indent the whole lot!

Regards,

Brian.