4 ms·
I really dislike this: {b: 0, c: 1} => {b:} p b #=> 0 It is so weird that a variable named `b` is initialized after an hash-key-like symbol is referenced
by izietto 6y ago
I really dislike this:
{b: 0, c: 1} => {b:}
p b #=> 0
It is so weird that a variable named `b` is initialized after an hash-key-like symbol is referenced (`b:`). It's something you've never seen in Ruby before and that I hope I'll never encounter along my career as Ruby on Rails developer.
- speedgoose 6y agoIt's becoming quite common in JavaScript, but usually with a const/let keyword in front so it's easier for the brain to understand that you are reading a destructuring assignment.
- izietto 6y agoExactly that, I use it all the time in JS. I can't see why Ruby can't adopt something like: params = { a: 1, b: 2 } { a: a } = params p a #=> 1
- latortuga 6y agoI believe you are misunderstanding how this new feature works in Ruby. You've brought up destructuring assignment in javascript which flows right-side values by key to left-side variables. In Ruby, this new syntax is "rightward" assignment, values flowing from left to right: 3 => x # assigns 3 to x [4, 5] => [x, y] # assigns 4 to x, 5 to y
- speedgoose 6y agoIndeed. It's very different. Thanks for the clarification.
- spaced-out 6y agoDoes anyone know the reasoning for adding this? The link doesn't discuss that. As someone who worked primarily in Ruby ~5 years ago, it just seems like useless complexity to me.