4 ms·
Sigh... why couldn't they drop the : like js? {x:, y:} is a syntax sugar of {x: x, y: y}.
by soheil 5y ago
Sigh... why couldn't they drop the : like js?
{x:, y:} is a syntax sugar of {x: x, y: y}.
- jaynetics 5y agoI guess because keyword arguments couldn't use the same shorthand if they did.
- BurningFrog 5y agoI definitely like the clear visible syntax
- alex_smart 5y agoBecause unlike js, ruby chose to use {x, y} as the set literal.
- tinco 5y agoOhh since when is that? I've never seen or used that syntax and I sort of like it.
- alex_smart 5y agoSorry I found this page on Google but did not read the note on top. Looks like it was a proposed feature but was not implemented. https://dev.to/baweaver/ruby-3-set-literal-3fp9 https://dev.to/baweaver/ruby-3-set-literal-3fp9 Unfortunately it looks like I can't edit or delete that comment now. :embarassed:
- nauticacom 5y agoThis isn't true; Ruby has no set literal. Sets are provided by a default gem but there is no built-in syntax for them.
- alex_smart 5y agoSorry I found this page on Google but did not read the note on top. Looks like it was a proposed feature but was not implemented. https://dev.to/baweaver/ruby-3-set-literal-3fp9 https://dev.to/baweaver/ruby-3-set-literal-3fp9
- pxeger1 5y agoOtherwise you couldn't drop the braces like you currently can when passing hashes to functions. x, y = 1, 2 puts x: x, y: y # ==> {:x=>1, :y=>2} puts x:, y: # ==> {:x=>1, :y=>2} puts x, y # ==> 1, 2 Which would be inconsistent and annoying. (and knowing Ruby, I imagine there might be some syntactical ambiguities as well)
- soheil 5y agoI'm ok with having x:, y: but couldn't they drop the : if there are braces to indicate this is a hash? {x, y}
- ljm 5y ago{ x }, depending on where you put it, could be a block that returns the value of x. 5.times { x } This could either call the given block 5 times, or pass a hash of :x => x as the first argument.
- soheil 5y agoAh thanks that answers my other question.