From: "David A. Black" Date: 2004-08-20T22:17:19+09:00 Subject: Re: splat behavior with different versions of ruby Hi -- On Thu, 19 Aug 2004, Brett Williams wrote: > This is probably common knowledge, but the references I could find where > annotated only in Japanese so I'm just not sure what is happening here: [code examples] > So out of 1.6.8, 1.7.2, 1.8.0, and 1.8.1, only 1.8.1 exhibits the > behavior that I find counterintuitive. Is this the way it is supposed > to work? If so, what is the rationale? There's a little bit on what might be a related topic at , and I think there's been some discussion on ruby-core. Here are what amount to some annotated notes to myself, which I hope are reasonably accurate in their explanations. I'm definitely still working my way through this. These are all 1.8.1. # Simple assignment to an array: $ ruby -e 'y = [1,2,3]; p y' [1, 2, 3] # Un-array the array: $ ruby -e 'y = [1,2,3]; p *y' 1 2 3 # This one means: "an unarrayed object (*y) is [1,2,3]", # so the object itself (y) must be [[1,2,3]] $ ruby -e '*y = [1,2,3]; p y' [[1, 2, 3]] # And this is the un-arrayed version of the above: assign # to *y, examine *y (the *'s essentially cancel out). $ ruby -e '*y = [1,2,3]; p *y' [1, 2, 3] Similarly, with a non-array object: # *y is a string: $ ruby -e '*y = "abc"; p *y' "abc" # *y is an un-arrayed string; y is the array-wrapped version $ ruby -e '*y = "abc"; p y' ["abc"] Given the above, one might say that *y always means: that which, when wrapped in an array, is y". That raises the question, however, of what happens when y is not an array. What seems to happen is that * becomes a no-op: # In this example, un-arraying the object("abc") is a no-op, # because it's already as un-arrayed as it can get. $ ruby -e 'y = "abc"; p *y' "abc" Also, the un-arraying process will not go so far as to dissolve the last level of array into a bare list. # *[1,2,3] is "that which, when wrapped in an array, is [1,2,3]" # but un-array down to a bare list isn't allowed; therefore: $ ruby -e 'y = *[1,2,3]; p y' [1, 2, 3] Whereas if there are enough levels to un-array the object comfortably, that's what happens: # That which, when wrapped in an array, is [[1,2,3]], # namely [1,2,3]. $ ruby -e 'y = *[[1,2,3]]; p y' [1, 2, 3] Hoping this isn't too wildly misleading.... David -- David A. Black dblack@wobblini.net