TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ruby adds experimental support for rightward assignments

107 点作者 im_dario超过 4 年前

35 条评论

jbackus超过 4 年前
Ruby&#x27;s parser is notoriously complex; if I remember correctly, only a few members of the core team even know how to maintain it without introducing regressions.<p>The craziest part of this is that Ruby does not provide a full featured Ruby parser, so its entire static and dynamic analysis ecosystem depends on a (actually very high quality) 3rd party parser, begrudgingly maintained by someone who (AFAIK) doesn&#x27;t even write Ruby anymore: <a href="https:&#x2F;&#x2F;github.com&#x2F;whitequark&#x2F;parser" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;whitequark&#x2F;parser</a><p>When I see new language features like this, I think of how Ruby&#x27;s entire tooling ecosystem depends on the dramatically underfunded (and therefore primarily goodwill) efforts of high output maintainers like whitequark and a few others. Ruby&#x27;s highly dynamic and untyped nature means these tools are all the non-runtime guarantees you can get, basically. Epitome of digital infrastructure.<p>Consider asking your company to fund some of these people:<p>* <a href="https:&#x2F;&#x2F;github.com&#x2F;whitequark" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;whitequark</a> (maintains parser)<p>* <a href="https:&#x2F;&#x2F;github.com&#x2F;sponsors&#x2F;bbatsov" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;sponsors&#x2F;bbatsov</a> (maintains RuboCop)<p>* <a href="https:&#x2F;&#x2F;github.com&#x2F;sponsors&#x2F;mbj" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;sponsors&#x2F;mbj</a> (maintains unparser and mutant)<p>---<p>As context, I know this stuff intimately because I used to contribute heavily to most static and dynamic analysis tools in the Ruby ecosystem (<a href="https:&#x2F;&#x2F;github.com&#x2F;backus?tab=overview&amp;from=2017-12-01&amp;to=2017-12-31" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;backus?tab=overview&amp;from=2017-12-01&amp;to=20...</a>) and used to track new ruby changes really closely: <a href="https:&#x2F;&#x2F;cognitohq.com&#x2F;new-features-in-ruby-2-4&#x2F;" rel="nofollow">https:&#x2F;&#x2F;cognitohq.com&#x2F;new-features-in-ruby-2-4&#x2F;</a>
评论 #24438354 未加载
评论 #24437538 未加载
评论 #24437665 未加载
kevin_thibedeau超过 4 年前
This hurts readability of code with lengthy expressions. Knowing the target of the assignment is usually more important than the particulars of the expression when you&#x27;re skimming unfamiliar code. Having the variable pushed out to random locations on a ragged right side isn&#x27;t helpful.
评论 #24434950 未加载
评论 #24434924 未加载
jaynetics超过 4 年前
To counter some of the complaints regarding readability, a major use case for this is simply hacking stuff together in the REPL. Sometimes you notice you want to have your result as a variable when you&#x27;ve just typed a line, and other solutions for this problem (Ctrl+A or ENTER plus foo = _) are not universal.<p>Disclaimer: I&#x27;m not a core dev but saw a discussion amongst them about such use cases.
评论 #24435506 未加载
评论 #24435774 未加载
评论 #24435396 未加载
iandev超过 4 年前
&gt; While the above pattern has become standardized, it feels somewhat unnatural as we read most of the spoken languages from left to right.<p>Is this true (the &#x27;unnatural&#x27; part)? Reading left to right, `age = 42` can be read as &quot;age is 42&quot; which feels perfectly natural in English. `42 =&gt; age` would be read as &quot;assign 42 to age&quot;? This feels more awkward to me. I&#x27;m not sure I understand why anyone would want Rightward assignment.
评论 #24437552 未加载
评论 #24435265 未加载
评论 #24435180 未加载
评论 #24435867 未加载
评论 #24435382 未加载
评论 #24435461 未加载
评论 #24437733 未加载
评论 #24435069 未加载
评论 #24435557 未加载
评论 #24438181 未加载
评论 #24435778 未加载
rsanheim超过 4 年前
I love Ruby. I&#x27;ve been working with it as my primary language for 15+ years.<p>Its sad to see how they keep adding new, arcane syntax for fairly obscure use cases, seemingly without any real clear justification. I wish Matz would raise the bar on these changes - Ruby is mature and already has a very powerful, complicated syntax that gets tricky for corner cases. I appreciate &quot;more than one way to do it&quot;, but not everything needs to have ten ways to do it when the first three ways work fine.
zevv超过 4 年前
Well, this is a nice coincidence, Nim also added support for rightward assignments today!<p><a href="https:&#x2F;&#x2F;play.nim-lang.org&#x2F;#ix=2x7r" rel="nofollow">https:&#x2F;&#x2F;play.nim-lang.org&#x2F;#ix=2x7r</a>
评论 #24436166 未加载
评论 #24436250 未加载
EsotericAlgo超过 4 年前
Here is a feature ticket. The usage in a data transformation pipeline feels right and appropriate as in comment #20 <a href="https:&#x2F;&#x2F;bugs.ruby-lang.org&#x2F;issues&#x2F;15921" rel="nofollow">https:&#x2F;&#x2F;bugs.ruby-lang.org&#x2F;issues&#x2F;15921</a>
评论 #24435525 未加载
nooyurrsdey超过 4 年前
This is just feature bloat. Why is this needed?
评论 #24437147 未加载
Animats超过 4 年前
Can you mix left assignments, right assignments, and assignments in expressions all in one statement? That will be great for obfuscating code.
评论 #24436224 未加载
bfuclusion超过 4 年前
The right way to do this is the way Prolog does it. It doesn&#x27;t matter if you say VAR = 12 or 12 = VAR or 1,2,10 = A, B, C It does whatever it takes to make the left and the right sides equivalent to each other.
评论 #24437310 未加载
jcheng超过 4 年前
Rightward assignment is pretty strongly discouraged in the R community, due to the effect it has of burying the lede. That said, I always felt the same about Ruby’s inline if&#x2F;unless (`puts &quot;still working&quot; unless done`) so maybe it will have a better reception in this community.
评论 #24435360 未加载
pqdbr超过 4 年前
I&#x27;m also not a fan of spending this syntax card on this feature. The ... =&gt; ... syntax might be needed for something more useful in the future.
drusepth超过 4 年前
More HN discussion on this topic from 6 days ago: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=24369892" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=24369892</a>
hhsnopek超过 4 年前
[question]: Is the main benefit of &quot;Rightward assignments&quot; readability and does this provide more benefits?
评论 #24435434 未加载
评论 #24434897 未加载
评论 #24435017 未加载
评论 #24435866 未加载
e12e超过 4 年前
Uh.. I&#x27;m not sure I like this, in a language that already has a similar boolean operator (although I suppose that goes for all uses of such &quot;arrow&quot; operators). But still:<p><pre><code> a = 2 b = 1 b &gt;= a # false b &gt;= a =&gt; a # assign false to a? b =&gt; a # assign 1 to a?</code></pre>
评论 #24437836 未加载
评论 #24439700 未加载
r00fus超过 4 年前
Is this really just syntactic sugar? None of the examples in the article seemed to show why code would be more readable (array assignment maybe, but the leftward seems just as readable).
wyuenho超过 4 年前
I&#x27;m sorry but this &quot;feature&quot; is yet another design a hacker living in a bubble came up with. Most people think and program in English, or languages very close to English. In English I can read let x be 4ys and some change. I don&#x27;t know how to read the arrow. 4ys and some change implies x? If this is a feature designed purely for speakers of SOV or OSV order languages like Japanese, that&#x27;s fine, but the dominant language in technical discourse isn&#x27;t one of those.
评论 #24437903 未加载
评论 #24437771 未加载
drchopchop超过 4 年前
So, a = b becomes equivalent to b =&gt; a.<p>However, you already construct hashes like hash = {:a =&gt; b}.<p>Perhaps we can also add reverse hash construction like hash = {b = a} for consistency (and additional madness)
lacker超过 4 年前
This is true to Ruby&#x27;s Perl heritage. There is more than one way to do everything, even the most basic of operations.<p>Quiz: what does the following Ruby code do?<p><pre><code> a = b =&gt; c = d</code></pre>
评论 #24438564 未加载
评论 #24436975 未加载
Igelau超过 4 年前
This would be nice for situations where you have a lot of method calls daisy chained together. Like builders or Java Streams.<p><pre><code> DoThing() .withSomeOtherThing() .onSundays() .withCharset(Blah.BLAH) .betterAddThis(x -&gt; { ret stuff() }) .build() =&gt; turducken </code></pre> The whole assembly of the variable &#x27;turducken&#x27; is drifting rightward. By the end we want to get to the point rather than backtrack.
评论 #24438256 未加载
dragonwriter超过 4 年前
Ruby, it should be noted, already has experimental support for rightward assignments by way of pattern matching, so this is <i>really</i> TMTOWTDI.
评论 #24437518 未加载
matheusmoreira超过 4 年前
I expected this new feature to use the -&gt; token but they went with =&gt; instead.<p>Is there any reason why they overloaded the hash rocket? Even the simple examples in the article look like somewhat malformed hashes to me!<p><pre><code> 3 =&gt; box_height [170, 65] =&gt; height, weight traits =&gt; height, weight </code></pre> How does the parser tell them apart?
评论 #24438961 未加载
lxe超过 4 年前
Why? Why does ruby have an infinite amount of ways to do the same thing?
评论 #24435305 未加载
jasonrojas超过 4 年前
When can we start writing ruby bottom up?
actuator超过 4 年前
Ugh! I love that Ruby is expressive but this will just add confusion when a developer moves to Ruby for the first time. It adds a mental burden to be aware about all these rules.
adamzapasnik超过 4 年前
I love Ruby and its 10th ways of writing code to achieve same thing...<p>Eh, there is a reason why Golang has such a huge popularity.
评论 #24435255 未加载
评论 #24438036 未加载
ww520超过 4 年前
I like it. The next step is piping result forward.<p><pre><code> 1.2 + 2 | double | abs =&gt; result</code></pre>
nchelluri超过 4 年前
i was kinda shocked to see all the negativity in this thread. i looked at this post and thought &#x27;beautiful&#x27; and kinda mind-blown at the simple elegance of it. ruby is one of those languages that was cutting edge in 1995 and is apparently cutting edge today. it writes and reads like nothing else. it can be made very, very, or too terse, but it can be made to read simply and concisely and clearly with an economy of characters spent. i also am not a huge fan of the metaprogramming madness. but ruby is a beautiful language and i definitely<p><pre><code> &#x27;think highly&#x27; =&gt; sentiment </code></pre> this post.
transfire超过 4 年前
I wish they pay more attention to real issues and potentials. e.g. The Refinement design is half-baked -- Can&#x27;t refine module methods, can&#x27;t DRYly reuse monkey patch code as refinements...
thysultan超过 4 年前
Left is right, bottom is up, and the world is upside down.
throwaway4007超过 4 年前
&gt;R language also has a similar way of doing this 42 -&gt; age.<p>Ah yes, the R language, a model of clarity, beauty and lightness in the world of programming languages. Truly an example to follow.<p>(And it&#x27;s not like anyone ever uses that &quot;feature&quot; in the R world either.)
评论 #24437417 未加载
评论 #24436437 未加载
评论 #24437282 未加载
kazinator超过 4 年前
Awk rightward assignment:<p><pre><code> $ awk &#x27;BEGIN { &quot;echo foo&quot; | getline var; print var }&#x27; foo</code></pre>
ericb超过 4 年前
I love ruby, but this is awful. Hope it goes away.
awwaiid超过 4 年前
Ummm. Feel like I&#x27;m being trolled.
dgellow超过 4 年前
But... why?