Live data from Hacker News

Ruby 2.7

ruby-lang.org

21–30 of 68 posts

Re: Ruby 2.7

#21
post #19
post #13

Does the pattern matching do something that Ruby's map() can't do? Coming from Perl, map() there can return fewer elements than the source list, so pattern matching works already. A short skim of Ruby's map seems to imply it always returns something with the same number of elements. Edit: I was confused about what this feature did. So this subthread is still interesting, but mostly unrelated.

Map is data manipulation, whereas pattern-matching is control flow.

Yes, thank you. Reading the sub linked presentation, I see it now.

The sentence "It can traverse a given object and assign its value if it matches a pattern" sounded like traverse and (optionally) manipulate.

Re: Ruby 2.7

#22
> Calling a private method with a literal self as the receiver is now allowed.

Oof. Call me old fashioned, but I liked the consistency of not being able to call private methods with an explicit receiver. Oh well!

The rest of this looks great, thanks Ruby team!

Re: Ruby 2.7

#23
post #15
post #14

Earlier quoted context omitted.

Yeah afaik something is always returned from the block (even if it’s nil). Occasionally I recall my self doing something where the block might return nil and then I’d call `compact` to get rid of extra nil stuff. It works when you want a modified version of the enumerable but don’t want nil stuff. If you just want to match a subset then I think `select` is probably what you want. Further, the pattern matching feature…

In JavaScript, Elixir, and Ruby I use flatMap for this purpose. You can return an empty array on the block to “skip” over the element.

2.7 introduces `Enumerable#filter_map` for that. Example from the NEWS file:

[1, 2, 3].filter_map {|x| x.odd? ? x.to_s : nil } #=> ["1", "3"]

Re: Ruby 2.7

#24
post #5
post #4

Earlier quoted context omitted.

I’d love to, if it helps with things like Either/Maybe types. Although from what I’ve looked at briefly the syntax felt a little less intuitive than I’ve seen in other languages. Will have to properly try it out.

what's the purpose of Either and Maybe in a dynamically typed language?

Pop an item from an array. What do you return when the array is empty?

Solution: nil. Problem: How do you tell the difference between "the array was empty" and "the item you popped was nil"?

Solution: have pop return a Maybe instead.

Re: Ruby 2.7

#25

Pattern matching is awesome in Elixir, it's great to see functional language traits stain the Ruby language more. It's all the better for it.

I was introduced to it in Scala. I like how all "classic" languages are cherry-picking features from functional programming. It will make people less shell-shocked in the future, when they try it for real.

Re: Ruby 2.7

#26
One of the interesting news is that they are now going to start requiring a C99 compiler, instead of only C90.

I've been considering to do the same on my own projects. What does HN have to say about this? Is anyone here still working in a context where C99 is not an option? Did anyone else also recently switch to C99? How did it go?

Re: Ruby 2.7

#27

Pattern matching is awesome in Elixir, it's great to see functional language traits stain the Ruby language more. It's all the better for it.

My first exposure to it was in Haskell. It felt very surreal and almost magical at the time.

Re: Ruby 2.7

#28

Pattern matching is awesome in Elixir, it's great to see functional language traits stain the Ruby language more. It's all the better for it.

Basically this is Elixir's case, so a very narrow instance of what Elixir's pattern matching is. I'd love to see more pattern matching added to Ruby year after year. However adding everything Elixir has probably would require a total rework of the language. Example: multiple method definitions, one per pattern.

I wonder what's going to be the idiomatic way to handle a failed match. Elixir ends the process but there is likely a supervisor to restart it. In the case of Ruby the process ends and there is nothing to restart it. In the case of Rails I expect the web request to fail with a 500 and maybe the next one will get more luck.

Re: Ruby 2.7

#29
post #5

Earlier quoted context omitted.

what's the purpose of Either and Maybe in a dynamically typed language?

Pop an item from an array. What do you return when the array is empty? Solution: nil. Problem: How do you tell the difference between "the array was empty" and "the item you popped was nil"? Solution: have pop return a Maybe instead.

or allow returning multiple values. for example in CL searching for a thing returns two values the is-found and the value

Re: Ruby 2.7

#30
post #26

One of the interesting news is that they are now going to start requiring a C99 compiler, instead of only C90. I've been considering to do the same on my own projects. What does HN have to say about this? Is anyone here still working in a context where C99 is not an option? Did anyone else also recently switch to C99? How did it go?

Why? Not being facetious. Wondering about the weights of benefits versus costs for users.
Post reply on HN