Live data from Hacker News

New Features in Ruby 2.4

blog.blockscore.com

61–70 of 76 posts

Re: New Features in Ruby 2.4

#61
post #10
post #4

Earlier quoted context omitted.

It has a historical reason: https://bugs.ruby-lang.org/issues/10617

To summarize* and elaborate: Originally in Ruby, multiple assignment inside a conditional caused a parse error. Why? Because multiple assignment originally always returned an array: irb(main): a, b = nil => [nil] In Ruby, non-empty arrays always evaluate to truthy. (The array above has one element, nil.) Hence, multiple assignment inside a conditional would have been meaningless. For this reason, Ruby threw a parse e…

puts "true" if []

An empty array is still an object, so tests to true. Similarly 0, etc.

The only false values are 'false', of course, and 'nil', the lack of anything.

Re: New Features in Ruby 2.4

#62
post #10

Earlier quoted context omitted.

To summarize* and elaborate: Originally in Ruby, multiple assignment inside a conditional caused a parse error. Why? Because multiple assignment originally always returned an array: irb(main): a, b = nil => [nil] In Ruby, non-empty arrays always evaluate to truthy. (The array above has one element, nil.) Hence, multiple assignment inside a conditional would have been meaningless. For this reason, Ruby threw a parse e…

puts "true" if [] An empty array is still an object, so tests to true. Similarly 0, etc. The only false values are 'false', of course, and 'nil', the lack of anything.

Good catch! If only other languages were as simple :D

Re: New Features in Ruby 2.4

#63
post #57

Earlier quoted context omitted.

Haven't tried it, but the assignment semantics is that the return of the assignment expression is always the right hand side of the operator. They probably kept that semantic. So foo, bar = ['foo', nil] evaluates to ['foo', nil] which is truthy. If the code was: if (foo, bar = [nil, nil]) 'truthy' else 'falsey' end It'd still be truthy even if no variables were "set" (they were actually set, explicitly to nil, but yo…

Interesting. In particular the result with both values being nil is something I would find pretty counter-intuitive.

Just gotta remember there's nothing special about there being an assignment there, it's just another expression. `if` is another expression and it doesn't care about the status of assignments or whatever is in the conditional clause, only what the expression inside evaluates to.

As far as the assignment operator evaluating to the right hand side, it'd be more difficult to understand if that wasn't the case. There are more scenarios to destructuring on the left hand side than the right.

Also keep in mind there's no such thing as a "failed" assignment. They always work, sometimes things are just assigned to nil explicitly or implicitly.

This case is somewhat hard to get because it brings together a few things of Ruby in way that can get weird. Those things separately are actually pretty good, but bunched up makes it a bit more dense semantically. Which is why it's best to avoid getting too clever with assignments in conditionals.

Re: New Features in Ruby 2.4

#64
post #6
post #2

Some of these are pretty amazing, like the `digits` method and the new OptionParser functionality (which seems to be a sort of standard library equivalent to doctopt!). That being said, I can't help but shake my head at the section about "multiple assignment of conditionals: "You can now assign multiple variables within a conditional...You probably shouldn’t do that though." Am I alone and thinking that it's a little…

Ah well "You probably shouldn’t do that though" is just my opinion as the author. I didn't play a part in these changes. I just like Ruby enough to dig into their changelog :)

Ah, okay, I confused this with an official recommendation. Thanks for the clarification!

Re: New Features in Ruby 2.4

#65
post #47

Why do the old match operators now implicitly set a global variable? I feel like in a year I'm going to be debugging some really weird race conditions because of that change and I can't see any value in it...if I want to use the value of a match operator I'll _always_ save it to a local variable. Can anyone shed any light on this, because my first attempts at grok'ing this change have me really blown away.

These globals are thread local and method local, so these are safe from race conditions.

Though, I too had to dig into it the first time I saw use of these special vars to gain confidence that I wasn't introducing a world of pain when we hit load.

Re: New Features in Ruby 2.4

#66

These are all great but when are we getting optional typing? If the js ecosystem wasn't a clown operation my language of choice would be typescript instead of ruby.

If you like types, why would you use TypeScript over Elm? http://elm-lang.org/

Because I like optional types. I don't like the type system dictating the entire design.

Re: New Features in Ruby 2.4

#67
post #14
post #6

Earlier quoted context omitted.

Ah well "You probably shouldn’t do that though" is just my opinion as the author. I didn't play a part in these changes. I just like Ruby enough to dig into their changelog :)

Interestingly, assignment inside conditionals is usually avoided because it is easy to accidentally do an assignment (=) rather than a comparison (==). Note that for multiple assignment this mistake can no longer happen: irb(main): a, b == 3 SyntaxError: syntax error, unexpected ==, expecting '=' Should it still be avoided? :)

First of all that is an interesting point. Given that it isn't as potentially dangerous I'll agree that it is less bad than I initially thought.

I'll respond to your question by asking you a question. Which of the following expressions evaluate to "truthy"?

    'truthy' if (a, b = [])              # =>
    'truthy' if (a, b = nil)             # =>
    'truthy' if (a, b = [nil])           # =>
    'truthy' if (a, b = [nil, nil])      # =>
    'truthy' if (a, b = [false])         # =>
    'truthy' if (a, b = [false, false])  # =>
    'truthy' if (a, b = [true, false])   # =>
    'truthy' if (a, b = [false, true])   # =>
    'truthy' if (a, b = *[])             # =>
    'truthy' if (a, b = *nil)            # =>
    'truthy' if (a, b = *[nil])          # =>
    'truthy' if (a, b = *[nil, nil])     # =>
    'truthy' if (a, b = *[false])        # =>
    'truthy' if (a, b = *[false, false]) # =>
    'truthy' if (a, b = *[true, false])  # =>
    'truthy' if (a, b = *[false, true])  # =>
The answer is in this gist: https://gist.github.com/backus/c9b70dee67470698fd7d4a66ddf03.... Don't peek!

Re: New Features in Ruby 2.4

#68

Earlier quoted context omitted.

If you like types, why would you use TypeScript over Elm? http://elm-lang.org/

Because I like optional types. I don't like the type system dictating the entire design.

I like optional types as well, but more because I'm lazy. The Elm and Haskell people are not complaining about types, and given that every function takes SOMEthing (of some implicit type) and usually spits out SOMEthing (of some implicit type), I can hardly see how it will "dictate the design" more than create a bit more validation code.

Re: New Features in Ruby 2.4

#69

Earlier quoted context omitted.

Because I like optional types. I don't like the type system dictating the entire design.

I like optional types as well, but more because I'm lazy. The Elm and Haskell people are not complaining about types, and given that every function takes SOMEthing (of some implicit type) and usually spits out SOMEthing (of some implicit type), I can hardly see how it will "dictate the design" more than create a bit more validation code.

Not having this flame war. I have my reasons, you have yours. We will continue writing code using our separate ways. Certain patterns are awkward or even impossible in statically typed languages and you need to bypass the type system to get things done so implicit or not, static type systems have a cost. There is a benefit as well but the kinds of systems I work on I don't need the extra validation. I need the ability to quickly prototype and ship and then layer on the validation, hence the need for optional types.

Re: New Features in Ruby 2.4

#70
post #67
post #14

Earlier quoted context omitted.

Interestingly, assignment inside conditionals is usually avoided because it is easy to accidentally do an assignment (=) rather than a comparison (==). Note that for multiple assignment this mistake can no longer happen: irb(main): a, b == 3 SyntaxError: syntax error, unexpected ==, expecting '=' Should it still be avoided? :)

First of all that is an interesting point. Given that it isn't as potentially dangerous I'll agree that it is less bad than I initially thought. I'll respond to your question by asking you a question. Which of the following expressions evaluate to "truthy"? 'truthy' if (a, b = []) # => 'truthy' if (a, b = nil) # => 'truthy' if (a, b = [nil]) # => 'truthy' if (a, b = [nil, nil]) # => 'truthy' if (a, b = [false]) # =>…

Good exercise! So it may still be too easy to misunderstand when exactly the condition will be true.
Post reply on HN