rubysyntaxconvention

Syntax for Block Local Variables


I am confused about a good style to adopt to define block local variables. The choices are:

Choice A:

method_that_calls_block { |v, w| puts v, w }

Choice B:

method_that_calls_block { |v; w| puts v, w }

The confusion is compunded when I want the block local to have a default value. The choices I am confused about are:

Choice C:

method_that_calls_block { |v, w = 1| puts v, w }

Choice D:

method_that_calls_block { |v, w: 1| puts v, w }

Is there a convention about how block local variables must be defined?

P.S. Also it seems the ; syntax does not work when I need to assign default value to a block local variable! Strange.


Solution

  • Choice B is not valid. As @matt indicated - it is a valid (though obscure) syntax (see here: How to write an inline block to contain local variable scope in Ruby?)

    Choice C gives a default value to w, which is a regular value, while Choice D is a syntax for default keyword argument.