93 points · by kaspth · 11y ago · 36 comments
> I don’t know when this is useful.
Ah! When I was learning Ruby after being a long time perl user, this was the one huge missing feature that I longed for. Perl splats automatically (you have to use refs if you don't want it to), and that leads to nice option construction:
@opts = ($some_flag1 ? (option1 => $some_flag1) : (),
$some_flag2 ? (option2 => { a => 1 }) : (),
$some_flag3 ? (option3 => [ 1,2,3 ]) : (),
$some_flag4 ? (option4 => [$some_flag4, 1]) : ())
But it was annoying in Ruby. The best way I found previously was with a bunch of chained .merges, one for each option: opts = {}.merge(some_flag1 ? {:option1 => $some_flag1} : {})
.merge(some_flag2 ? {:option2 => { :a => 1 }} : {})
.merge(some_flag3 ? {:option3 => [ 1,2,3 ]} : {})
.merge(some_flag4 ? {:option4 => [$some_flag4, 1]} : {})
Ugly and verbose compared to the perl (which is usually not the case in perl vs ruby). This new syntax allows for: opts = {**(some_flag1 ? {:option1 => $some_flag1} : {}),
**(some_flag2 ? {:option2 => { :a => 1 }} : {}),
**(some_flag3 ? {:option3 => [ 1,2,3 ]} : {}),
**(some_flag4 ? {:option4 => [$some_flag4, 1]} : {})}
Which looks only marginally better, but allows nice things like interleaving optional and non-optional arguments: opts = {**(some_flag1 ? {:option1 => $some_flag1} : {}),
:option1_related => "xyz",
**(some_flag2 ? {:option2 => { :a => 1 }} : {}),
**(some_flag3 ? {:option3 => [ 1,2,3 ]} : {}),
:option4_related1 => "xyz",
:option4_related2 => "pdq",
:option4_related3 => "abc",
:option4_related4 => "123",
**(some_flag4 ? {:option4 => [$some_flag4, 1]} : {})}
That's quite nice. new Promise((resolve, reject) => {
...
resolve();
}); Promise.resolve();
[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...http://briancarper.net/blog/579/keyword-arguments-ruby-cloju...
This reminds me of everything I love, but also everything I hate, about Ruby. He writes:
------
With even more added sugar, you can leave off the parens in Ruby function calls. So this is pretty common in Ruby:
foo :x => 123 # => {:x=>123}
How nice and punctuation-less. But then things get ugly. What about this?
foo {:x => 123}
That won't even compile.
You can do `foo d: {:y => 2}` or `foo :d => {:y => 2}` but not a raw hash literal without parens.
That's parse.y[1] for you...
So sad to see a language stagnate like this.
It was mostly syntactical, and it didn't do much to improve readability.
I guess I was hoping to see functional improvements, where they address the pain points of ruby: performance, tooling, dependency hell, unicode support, lack of things like co-routines or generators, the ability to restrict access to internals.
What's odd is that "performance, tooling, dependency hell, unicode support, ... co-routines ... generators" reads like a list of things that have been worked on heavily since 1.8. Performance has been a major focus of every release since 1.9.1; integrating Rubygems considerably helped with tooling and dependency issues; encoding-aware strings were a major feature of 1.9; coroutines and generators can be implemented more effectively with 1.9's Fibers.
Performance and Unicode other people have mentioned alreaedy, and have been areas of improvement. Explicit library support for semi-coroutines and coroutines was added in 1.9 via Fiber and Fiber::Core, and Ruby already had generator support in 1.8.
So many of the things you say you were hoping to see have been addressed since 1.8, and at least one was already addressed in 1.8.
I used Rails when it was still `config.gem` with no dependency resolution, and it was an absolute nightmare, so I think I know what you're talking about here. But then bundler came along and, well, completely solved the problem.
```
enum = Enumerator.new{|y| a = 0; loop{y << a; a += 1}}
enum.next => 0
enum.next => 1
etc
```
I believe this has existed since 1.8x