9 ms·
Pattern Matching in Ruby 2.7 (2019)
- sawyerjhood 7y agoIs it just me or does the syntax seem very confusing? Compared to other languages with pattern matching (ex OCaml) it seems a little clunky. I'm not familiar with Ruby though, does this syntax mesh with the rest of the language though?
- rubyn00bie 7y agoIt mostly does, but they talk about part of that actually, and why they had to use and extend `case` rather than introducing a new reserved word (like match). And to your point, I think... Some of these features like pipes are really cool to see in languages like Ruby or JavaScript, but holy shit without true immutability some of this seems like a nightmare to emulate (like not being able to match on method calls if I remember correctly) in an traditional OOP runtime/vm. Fuck my life, and this is strictly IMHO, but pipes seem totally worthless when you have to worry about the object and not the data...
- pmontra 7y agoIn a language like Ruby and most imperative mutable ones pipelines could be a nice syntactic sugar for constructs like this a = f1() a = f2(a) a = f3(a, b) a = f4(a, c) It becomes (let me use Unix pipes) a = f1() | f2() | f3(b) | f4(c) OO idiomatically handles that by making each method return an object with the status and at least the next method to be called a = f1().f2().f3(b).f4(c) However unless the class of that object is something standard and already available like (example) Ruby's Enum, it's a pain to code it (and understand it later on) and people go for the shortcut of writing the code at the beginning of my reply.
- pmontra 7y agoIt kind of meshes with but I agree it's clunky. I'd like to have full pattern matching like we have in Elixir or at least in method definition. But that's going to turn Ruby into something different. I'm not sure it's worth it.
- xlii 7y agoI personally love pattern matching (one of the best features of Elixir and Erlang for my taste) and would be screaming in enjoyment if something similar would be introduced to Ruby but I'm kind of meh about the whole ordeal. This is experimental. Even when we upgrade production systems to 2.7 I'm still going to refute usage of pattern matching in the code (and it still boggles my mind why add experimental feature to a public release of such mature language as Ruby) since there is no guarantee we won't have to rewrite everything back in 2.7.4 or 2.8 The other issue I'm having with it is that it is connected to a case statement. It might be just me, but I'd dream of having this in function header definition instead of a statement which I rarely see in code and am not too comfortable to use as well (this might be connected to the crowd I'm usually working with).
- RangerScience 7y ago> in function header definition Hmmm, I can kind of see it, but then you'd also need function overloading, right? With this you'd just do outer/inner functions, where the outer determines the pattern match and dispatches along to the appropriate inner function. If you want to just make sure the function only accepts arguments that match a pattern, you're basically talking a guard pattern. Maybe a `return unless foo in <pattern>` syntax would then be nice (or some other one-liner), but it's not like it'd be hard to do it using this feature, you'd just use a few more mostly empty lines.
- verttii 7y agoYou mean that guards + destructuring would effectively be the same as pattern matching in function's params? Pattern matching would allow matching against more complex data structures more comfortably though.
- chucke 7y agoRefinements was also at some point marked as "experimental", and jruby maintainers have at some point declared not wanting to support it. It's 2020, refinements are still here, and jruby supports them, even with all its imperfections. I'm wondering: how would you showcase an experimental feature to the community? Cuz if you're saying "3rd party library", there were already 3/ 4 different pattern matching libraries.
- pcr910303 7y agoI hope... this is not another feature that is added to Ruby at the sake of the functional programming trend. Rubycop’s author criticized[0] Ruby 2.7 for losing a direction/vision where Ruby should go (which sparked controversial operators that were quickly removed). Most of them looks like, at least to me, trying to fit a functional programming features in an inherently object-oriented language to be ‘hip’. Like... the pipeline operator which had strange semantics. I hope this isn’t one. [0] Ruby, Where do We Go Now?: https://metaredux.com/posts/2019/12/06/ruby-where-do-we-go-now.html https://metaredux.com/posts/2019/12/06/ruby-where-do-we-go-n...
- RangerScience 7y agoHmmm. I'm definitely in favor of more functional programming features / capabilities, particularly in a language like Ruby that allows for pretty effective mixing of OOP/functional paradigms. Right tool for the job, where the job is usually "communicating with other programmers what this code should do". I've always found Ruby to be an amazing mix of functional & OOP already, what with blocks, and that class definitions are also (actually?) functions themselves. And don't forget being able to drop parens! I've also always felt like Ruby had mostly clear vision - arguably better than a perfectly clear one, since it leaves more room for play, exploration, and other people's contribution - even if it's not one that's able to be put into words. Every time I came across something surprising in Ruby, once I learned what was going on, it made sense in that larger whole.
- zelly 7y agoRuby blocks are another example of something that looks like a functional programming feature but is actually not. As in Java 8 and C++11, which also added lambdas, it doesn't have much in common with FP lambdas. These are essentially syntax sugar for an ad-hoc function that's too small to factor our and give a name. FP lambdas are the most elementary unit of computation. You're supposed to be doing absolutely everything with lambdas even addition, theoretically (head in the clouds academic BS), and the other syntax (like `do` in Haskell) is sugar for lambdas. So basically Ruby is far from FP. It sticks to OOP, which has actually been proven to work.
- RangerScience 7y agoYes! This is fantastic! And, in typical Ruby fashion, I've learned more about pattern matching from these slides than anything else trying to explain it that I've encountered before. I'd definitely vote in favor of the pinned expressions. One of the things I like about Ruby is how pretty much everything is an expression, and you can replace pretty much any symbol with an expression.
- meesterdude 7y agoI mean I get it - but, it doesn't feel like ruby to me. It lacks clarity. Maybe it's just me, but i'm not psyched to see this. It feels like a scope creep of some sort.
- emj 7y agoLacking clarity is a good word for it, it was my feeling as well, but I've not used Ruby extensively. My feeling trying to hack existing Ruby code was always that Ruby favoured the arcane abit too much. This is from a perspective of me not bothering to learn Ruby at all. I remember thinking the same about C and Python before I bothered learning them. I guess that feeling is always present when you try languages that are new, or our ignorance of pattern matching/Ruby?
- onli 7y agoThe ^ operator is maybe a bit confusing, and that Hashes need to use symbol keys to work here is just another example that symbols are a mess. But did you see the JSON-matching example in the slides? That made it totally clear to me how this is supposed to work and what the advantage of having this would be. Very powerful, very useful, and the code in that scenario is much clearer.
- nnq 7y ago...semi off-topic, but man, how I wish Ruby had won the hearts of data-science and machine-learning practitioners instead of Python or R! It looks like such a wonderful and extensible and programmer friendly language compared to all the others in the "multi-purpose dynamic 'scripting' languages class". Every time I read Ruby code it almost puts a smile on my face, it seems such a joy to write and think in, so sad it didn't break out of its niche much :(
- skrebbel 7y agoSame! I never used Rails much but did learn Ruby and used it a lot for the kind of stuff people tend to use Python for nowadays. Data crunching, prototyping, sciency stuff.. It's excellent for that. I mean, Ruby is so much more elegant and consistent than Python, I'll never understand how Python became the more widely appreciated language of the two, especially with the effective head start that Ruby had through Rails' popularity.
- stanislavb 7y agoI guess the answer is - Google. As it used Python and the word was out. Also, Python had been released a few years earlier and so was adopted by universities.
- jhanschoo 7y agoFrom my perspective, it was the was the ipython notebook (now jupyter) that was the killer app leading to Python being adopted for data science.
- kjeetgill 7y agoI'd say python was pretty big in academic circles even before that if memory serves.
- cdavid 7y agoGoogle had little influence on scientific python. I agree python did not have technically much advantage over ruby, but: 1. Old history of using python for scripting linux, starting from mid-late 90ies, especially around clusters and HPC, i.e. where you find applied scientists in academia. 2. A full C API. Not especially good, but powerful enough. I don't think ruby is much different, but wrapping C with python, again for HPC (or GUI libs). Lua C API is definitely better. 3. Communities matter 4. Different people starting to talk to each other more and more, and right timings. First mid 90ies (numeric/numarray, use in astronomy, etc.), then early-mid 2000ies (NumPy/SciPy consolidation, matplotlib, ipython), then late 2000 with scikit learn, and pandas. Also, while I know little about ruby, I fail to see how much better it is than python. Both are essentially the same w/ some minor syntactic differences, and different communities. I have never seen an example where ruby was much better than python or vice versa for non trivial code. I mean is RoR that much better than Django ? I doubt that stuff matters very much.
- louis8799 7y agoDo we have something similar in python, if not I am going to make one.
- L3viathan 7y agoThere is this: https://github.com/grantjenks/python-pattern-matching https://github.com/grantjenks/python-pattern-matching
- Stormwalker 7y agoThis project is just someone wrote a proposal or something. Here is actually implemented pattern matching as a library https://github.com/santinic/pampy https://github.com/santinic/pampy
- pansa 7y agoIIRC Guido himself is planning on adding a `match` statement to Python.
- rkachowski 7y agoIs pattern matching implemented sequentially in order of declaration in other languages? My naive brain took the pattern resolution to be based on some kind of map of types and values to implementation.
- verttii 7y agoIt is, from top to bottom.
- LessDmesg 7y agoRuby... Someone still uses that in 2019?
- 3pt14159 7y agoI'm 80% in favour of this change. JSON parsing is tedious and this looks like it will help pretty cleanly on something that rubyists do quite frequently. I could also see myself using this pretty frequently elsewhere. That said, I do see some minor drawbacks: 1. I personally find the case statement in ruby confusing. It uses === which I find hard to use without stopping and thinking. 2. I don't understand why the bitwise or operator is being used for the pattern matcher or. Was it not possible to just use `or` itself? 3. Deconstruct keys reads a bit weird. Easy enough to understand and remember, but I can imagine being new to ruby or programming and finding the pattern matching pretty hard to follow.
- floki999 7y agoI used both Python and Ruby extensively for a while, love both, but Ruby was just nicer to code in. The problem for me was the much smaller ecosystem, especially for anything numerical/data related. Numpy was, pre Pandas days, the killer library for the type of work I do.
- vmchale 7y agoML is changing the world.
- Scarbutt 7y agoWrong thread?