-----Original Message-----
From: Calvin Bornhofen <calvin.bornhofen / web.de>
Sent: 20 September 2012 14:54
To: ruby-talk ML <ruby-talk / ruby-lang.org>
Subject: Splat operator and different options to pass a block

Hello,

I'm currently experimenting a bit in Ruby, to get a feeling of how the=20
language works. Out of curiosity, I wanted to have a method which would=20
accept a Proc ( or block/ lambda/ Method ) both passed attached or as an=20
explicit parameter. My idea to realise this was:

   def test( block_as_parameter=3D nil, &block_attached )
     block =3D block_attached || block_as_parameter
     block.call
   end

   test { puts "Foo" } #=3D> Foo
   test( Proc.new { puts "Foo" } ) #=3D> Foo

So far, so good (I even think that this is the easiest way to achieve=20
said functionality). This works as long as I don't have the splat=20
operator in my parameter list. There are - afaict - two reasons for this:
1.: After the splat operator it is not possible to specify optional=20
parameters (and even if we had the block_as_parameter parameter before=20
the splat, it would take precedence over it, resulting in 2. - also, it=20
would destroy interface consistency).
2.: The block_as_parameter parameter will be filled with one of the=20
parameters which we wanted the splat operator to gather.

My code to solve this problem is as follows:

   def test2( foo, *args, block_as_param, &block_attached )
     block =3D #this is where the magic happens
       if block_attached
         args << block_as_param
         block_attached
       else
         block_as_param
       end
     p foo
     p args
     block.call
   end

   test2( "FirstParam", "SecondParam" ) { puts "Foo" }
   #=3D> "FirstParam"
   #   ["SecondParam"]
   #   Foo
   test2( "FirstParam", "SecondParam", "ThirdParam" ) { puts "Foo" }
   #=3D> "FirstParam"
   #   ["SecondParam", "ThirdParam"]
   #   Foo
   test2( "FirstParam", "SecondParam", "ThirdParam", Proc.new { puts=20
"Foo" } )
   #=3D> "FirstParam"
   #   ["SecondParam", "ThirdParam"]
   #   Foo

This solution works as I want it to work, so that's good. But now I'm=20
curious: Is there another, easier way to achieve what I want? I'm aware=20
that this has not that many real-life usages, but I t

[The entire original message is not included]=