Blocks and variable parameter lists 
Author Message
 Blocks and variable parameter lists

If I define a block thus:

           { |a|   ...    }

Then 'a' is either a single value or an array, depending on whether it is
called with one or more than one value. Is there a syntax to make it always
be an array even if called with only one parameter? Or do I have to do

    a = [a] unless a.kind_of? Array

Thanks,

Brian.



Sat, 27 Aug 2005 21:34:29 GMT  
 Blocks and variable parameter lists

B> Then 'a' is either a single value or an array, depending on whether it is
B> called with one or more than one value. Is there a syntax to make it always
B> be an array even if called with only one parameter? Or do I have to do

pigeon% ruby -e '1.times {|*a| p a}'
[0]
pigeon%

pigeon% ruby -e '{"a", "b"}.each {|*a| p a}'
["a", "b"]
pigeon%

Guy Decoux



Sat, 27 Aug 2005 21:39:07 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. External Call with variable parameter list

2. Variable sized parameter lists.

3. Ada 0y wish list: parameters of package parameters

4. local variable and local variable in block behave differently

5. Variable Record, Variable Block

6. Turning a list value in a parameter into a list for eval without quasiquote/unquote

7. Hiding block parameter from super method

8. Specifying local and external block parameters (that old chestnut)

9. Specifying local and external block parameters (that old ches tnut)

10. confused about scope of block parameter

11. Hash.each block parameters

12. Extension Question: Block Parameters

 

 
Powered by phpBB® Forum Software