Live data from Hacker News

Ruby adds experimental support for rightward assignments

blog.saeloun.com

61–70 of 172 posts

Re: Ruby adds experimental support for rightward assignments

#61
post #57

Well, this is a nice coincidence, Nim also added support for rightward assignments today! https://play.nim-lang.org/#ix=2x7r

Funny you should mention it, raku just got the same feature!

https://tio.run/##K0gtyjH7/7@4NEkhMy8ts8Lq0Gpbu0O7NVQSdRRUgG...

Re: Ruby adds experimental support for rightward assignments

#62
post #49
post #7

> While the above pattern has become standardized, it feels somewhat unnatural as we read most of the spoken languages from left to right. Is this true (the 'unnatural' part)? Reading left to right, `age = 42` can be read as "age is 42" which feels perfectly natural in English. `42 => age` would be read as "assign 42 to age"? This feels more awkward to me. I'm not sure I understand why anyone would want Rightward ass…

But “age is 42” is a statement/Boolean, not a mutation. “Let age be 42” is more natural, but doesn’t sound well on reassignments (“let age now be 42”?)

I would say this as "set age to 42".

Re: Ruby adds experimental support for rightward assignments

#64
post #12

Why? Why does ruby have an infinite amount of ways to do the same thing?

As someone else said in these comments, Ruby offers multiple ways to express things, so you have more options to "write how you think" as opposed to being pushed to "think how the language wants you to write". I'm not saying it's good or bad, I'm just saying that appears to be one of the reasons for it.

It's great if you're the only one who ever has to read or maintain your code.

Re: Ruby adds experimental support for rightward assignments

#65
post #57

Well, this is a nice coincidence, Nim also added support for rightward assignments today! https://play.nim-lang.org/#ix=2x7r

Alright, I did in fact chuckle, out loud.

But I gotta ask: does it have the correct precedence?

And sure, throwing some superficial math at the problem works. But is that a coincidence?

I guess what I'm asking is: how does Nim know that `=>` is infix? How does it derive the correct precedence? It looks like spooky magic, but Nim is pretty well-designed in my experience: what's going on here?

Re: Ruby adds experimental support for rightward assignments

#66
post #57

Well, this is a nice coincidence, Nim also added support for rightward assignments today! https://play.nim-lang.org/#ix=2x7r

Funny you should mention it, raku just got the same feature! https://tio.run/##K0gtyjH7/7@4NEkhMy8ts8Lq0Gpbu0O7NVQSdRRUgG...

Doesn't Raku essentially already have this with feed operator? [1]

1: https://raku.guide/#_feed_operator

Re: Ruby adds experimental support for rightward assignments

#68
post #23

Earlier quoted context omitted.

A math tutor of mine was actually pretty angry at computer science for writing things like `a=1; a=2; //so 1=2 ?` I guess he'd rather have the "assign 42 to age"... Except he'd probably read `42 => age` as "42 implies age", which would probably cause him more sleepless nights ;)

He’d probably like a := 42 which is in a lot of languages actually! Also Erlang and other fp’s do consider = equivalence.

I noticed a year or two ago when I'm pseudocoding I seem to default to "age <- 42" which I kinda like more...

Re: Ruby adds experimental support for rightward assignments

#70
post #28

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've just typed a line, and other solutions for this problem (Ctrl+A or ENTER plus foo = _) are not universal. Disclaimer: I'm not a core dev but saw a discussion amongst them about such use cases.

This is the exact reason I've wanted this, for a long time. I even asked Matz about it a conference once, probably 10-ish years ago.

This is a completely unnecessary feature. `_` is always assigned to the value of the previous expression.

    >> 4 + 2
    => 6
    >> _
    => 6
    >> "abc" + "def"
    => "abcdef"
    >> _
    => "abcdef
Adding this kind of syntax for something that's already trivial to do is honestly kind of distressing for me as a Ruby developer who's been around since early 1.8.

I still strongly believe that the `key: value` hash syntax was a massive mistake. That's a lost battle, but this is getting ridiculous.

Post reply on HN