Live data from Hacker News

Ruby adds experimental support for rightward assignments

blog.saeloun.com

81–90 of 172 posts

Re: Ruby adds experimental support for rightward assignments

#81
post #31
post #6

Earlier quoted context omitted.

One of Ruby's philosophies is to provide multiple ways to express the same fundamental operation so that it can conform to your thoughts instead of forcing your thoughts to conform to it. You can still do it the old way where it makes sense. Whether this philosophy is helpful or harmful is another debate.

synonyms in ruby seem to be a mistake. Searching code for certain usage patterns ends up needing more complicated regular expressions taking into account all synonym variations. If you've seen a method one way many times while learning, then see a synonym, you end up having to search up docs to make sure there isn't some subtle difference.

I find it really valuable to have different patterns of similar tokens to express different intent. My canonical example is early returns in JavaScript.

Function f () {

  if(errorCindition) return

  if(normalBehavior) {
    return 1
  }
  else {
    return 2
  }
}

(I know, my use of braces and lack of semi-colons will cause flame wars!)

The error condition looks different from the rest, so when I’m scanning quickly, I know where I am. My eyesight’s rubbish, so the shape of the code has a marked impact on me. I don’t have to read the whole thing to orient myself.

I thought it was nuts when I read the title, but reading the examples had a noticeable “easing” effect on my reading. So I’m very excited by this addition to Ruby. It could lead to some neat, expressive patterns.

Re: Ruby adds experimental support for rightward assignments

#82
I love Ruby. I've been working with it as my primary language for 15+ years.

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 "more than one way to do it", but not everything needs to have ten ways to do it when the first three ways work fine.

Re: Ruby adds experimental support for rightward assignments

#83
post #73

This is true to Ruby's Perl heritage. There is more than one way to do everything, even the most basic of operations. Quiz: what does the following Ruby code do? a = b => c = d

Makes me reject the PR for lack of clarifying parentheses.

Re: Ruby adds experimental support for rightward assignments

#85
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?

> how does Nim know that `=>` is infix?

I expect it's the `

Haskell works the same way.

Re: Ruby adds experimental support for rightward assignments

#86
post #66

Earlier quoted context omitted.

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

Not quite. For example:

  > my $x
  (Any)
  > 1 ==> $x
  (1)
  > 2 ==> $x
  (2)
  > 3 ==> $x
  (3)
  > $x
  [1 2 3]
If ==> were simply assignment, we would expect x's value to be 3.

Re: Ruby adds experimental support for rightward assignments

#87
post #79
post #24

Earlier quoted context omitted.

Interesting, that it became discouraged. Probably won’t make it into Ruby proper then. I don’t fault them for experiment.

Actually, this is a pretty good reason, and it's straight from the source (creator of S, the predecessor of R): https://stackoverflow.com/a/51548423/139922 > When you type a long expression only to remember at the end that it would be a good idea to save the result, a right-hand arrow allows you to perform an assignment without retyping the line. (I don't do this personally, though. I just run the expression and then…

I use right assignment in this way all the time, you should try it Joe :)

Re: Ruby adds experimental support for rightward assignments

#88

This is just feature bloat. Why is this needed?

Because after a few thousand hours of ruby, when you can - most of the time - tell a variable form a function, or if a function is receiving a list of argument or a hash, you now need another challenge.

Re: Ruby adds experimental support for rightward assignments

#89

>R language also has a similar way of doing this 42 -> age. Ah yes, the R language, a model of clarity, beauty and lightness in the world of programming languages. Truly an example to follow. (And it's not like anyone ever uses that "feature" in the R world either.)

I don't understand what point you are trying to make here. Do you not like the feature simply because R has a similar feature? Why not look at examples given here and by others and decide/comment on whether it adds clarity or expressiveness or not?

Re: Ruby adds experimental support for rightward assignments

#90

Earlier quoted context omitted.

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 seems the opinionated, directive "there's only one way to do it" approach is winning over the hacking around, "wrapping around your mind" approach. Lisp and Perl have had their times under the sun, now it's all about Python and Go. Perhaps these things are cyclical.

I'm just happy that there's still a popular language around that lets me write how I think (or however _I_ think would make the code look the best), so I don't have to suffer through using languages that direct how I have to write every little thing.

Having both options is nice!

Post reply on HN