Parallel assignment in Ruby – gotcha

1. x, y = 1, 2 results in x = 1 and y = 2
2. x = [1, 2, 3] results in x = [1, 2, 3]
3. x, = [1, 2, 3] results in x = 1

So, what would
x, z = [1, 2, 3] result in? x = 1 and y = [2, 3]? I thought so, but no it doesn’t. It results in x = 1 and z =2.

Update: This is where the splat operator (*) comes into picture.

Blogged with the Flock Browser

Tags: , ,

Advertisement

Post a Comment

Required fields are marked *

*
*

Follow

Get every new post delivered to your Inbox.