S
Stedwick
I sometimes see funcs declared with def fun (blah, *args)
What does the *args do? Thanks!
What does the *args do? Thanks!
Philip said:I sometimes see funcs declared with def fun (blah, *args)
What does the *args do? Thanks!
Got it, thanks. I love the term "splat" lol. Personally, I'm a big fan
of passing hashes {} as arguments. <http://www.philipbrocoum.com/munch/>
You can even do multiple assignment this way:
b, c, d, e = *a
b # => 1
c # => 2
d # => 3
e # => 4
b, c, d, e = a # => [1, 2, 3, 4]
b # => 1
c # => 2
d # => 3
e # => 4
Arlen said:It's worth noting that this behaviour can be achieved without the splatYou can even do multiple assignment this way:
b, c, d, e = *a
b # => 1
c # => 2
d # => 3
e # => 4
operator:b, c, d, e = a # => [1, 2, 3, 4]
b # => 1
c # => 2
d # => 3
e # => 4
I don't like this inconsistency in the language. Left is a list, right should be a list. Making it mate with both list and array can introduce tricky inconsistencies. I am in favor of dropping the latter and always'd require splat for multiple assignment. I think that is _too_ dynamic for an untyped, dynamic language like Ruby.
Philip said:I think it makes perfect sense. If you are doing multiple assignments,
do this:
a = b = c = "hi"
But it's pretty cool to be able to do this:
a, b, c = ["hi", "hello", "wassa"]
Mark said:The problem is: what is the value of a after:
a, b, c = d
?
The answer is: it depends. If d is an array, then a becomes d[0],
otherwise a becomes d. Since you can write:
a, b, c = "hi", "hello", "wassa"
setting each value, and:
a, b, c = "hi"
Further, consider this example:
a = [[1, 2, 3]]
b, c, d = *a
What is the value of b? Since the multiple assignment is "equivalent"
to:
b, c, d = [1, 2, 3]
it would seem that the way arrays automagically expand would result in b
becoming 1, but actually b becomes the array [1, 2, 3] meaning that
automagic expansion is not happening. So even the inconsistency is
inconsistent!
The semantics of assignment should not change just because the class of
something on the rhs changes.
Also, out of curiosity, if this
a, b, c = [1, 2, 3]
does not mean this
a = 1
b = 2
c = 3
then what on earth should it mean?
implies
a = [1,2,3]
b = nil
c = nil
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.