Ruby adds experimental support for Rightward assignments
blog.saeloun.com
blog.saeloun.com
EDIT: I also completely forgot about the existing hashrocket syntax for maps.
The operator is written "->" and is informally called a "gazinta" ("goes into").
Before:
result = case value
when 5
...
when 4
...
end
After: case value
when 5
...
when 4
...
end => result
The latter allows for some nicer formatting I think.Not necessarily more complex, but not less complex either IMO. I often say complexity can’t be eliminated, only spread around differently; I think we’re seeing that here.
2 ways to do something is more complex than 1.
But it's more than just the syntax of the code. It now means new lint rules, new coding standards in projects (some will allow => assignment, some will encourage conformity to legacy code, and others will disallow it entirely). There will be new Rubocop rules and other editor configs to update for proper syntax highlighting.
Syntactic changes like this with little obvious benefit need to be looked at skeptically. Just because something can be done, doesn't mean it should be.
Or this one
cat => person.pet
{ "age" => age, "name" => name }
I like the idea a lot. It'd probably allow for some nice functional looking code, but that's also the problem... it'd only look functional. And I think without VM/compiler/runtime support of functional programming it's pretty much a big waste of time.
I can use piece shit for a shoehorn; doesn't mean I should.
I've been using ruby for a decade and I don't get why this is necessary (or even good).
def foo(*arg, **kwarg)
puts "#{arg}/#{kwarg}"
end
b = 'meh'
foo(:a => b)
Now this produces []/{:a=>"meh"}
But with right-assign [:a]/{}
?On the other hand Ruby has already such a extensive syntax that it's hard to come up with something else that would not already be taken. -> represents lambda, >> is a valid method name, etc.
"foobar" => upcase() => x
On an unrelated side note, I've always thought "=" was a huge mistake in language design, confusing countless people who are coming into programming who reasonably mistake it for equality. I don't know why it won over ALGOL-style ":=".
Probably because adding a colon to an equality sign doesn’t clarify the meaning at all.
Had Algol (as some less widely influential for other reasons languages have) adopted something that more clearly represented movement into the variable being assigned like:
a <- 25
Maybe it would have won out over “=" for assignment. a <- 25
and this: a < -25
Since everything was an expression in the language, the latter would evaluate to a boolean. In the end I just went with "=" for assignment, and "==" for equality.E.g.
a = 2
a = 3
2 = 3 //what???
Assignment is fundamentally part of the language execution model, which is entirely apart from anything related to mathematical equality. Even in languages where you can't rebind a variable name, it's still weird to use = as assignment, since it would have such different behavior than seemingly analogous operators like > and <.I'd wonder how the right assignment would work differently. What small gotcha would it create.
I also wonder how my co-workers would take it, if I started using it.. Or if I figured out how it was different and used it for the once case that it worked.
For me. having a million ways to do something leads to slower dev time. More context I'd need to load.
I was just teaching Ruby to someone learning to code. I have to admit because all popular languages use leftward assignment it was one less thing to teach.
=> is pretty loaded as it is
In my limited experience, leftward assignment breaks the “flow” explaining a program to a novice.