Syntax matters... starred parameters

October 25, 2006 by pedro mota

def foo(prefix, *all)\n  for e in all\n    print prefix, e, ' '\n  end\nend\n\nfoo('val=', 1, 2, 3) => val=1 val=2 val=3\n\nThe use of * allowed all the arguments from 1 to be collected into an array, used as a formal parameter named *all and passed to the method. Without any change to the method we could call foo('val=', 7, 12) or foo('val=', 6, 4, 2, 7, 5, 43), this is, with an arbitrary number of arguments.

© 2018 | Follow on Twitter | pedro mg & Hugo