|
|
|
|
|
by bonquesha99
2747 days ago
|
|
Thanks for your feedback! Check out this other proof of concept demonstrating ES6 style object destructuring in Ruby: https://github.com/lendingHome/destruct I think this same type of concept could be applied to port Elixir style pattern matching as well e.g. data = {
name: "John Smith",
age: 35,
prefs: {
lang: "en",
tz: "UTC",
}
}
User = Pattern { name age prefs[lang] }
user = User =~ data
user.name
user.age
user.lang
[data].map(&User)
case object
when Pattern { some attrs[:nested][real][deep, fields] }
Pattern!.some
Pattern!.real
Pattern!.deep
Pattern!.fields
Pattern!.nested #=> NoMethodError
end
# or define "locals" by defining temporary methods on
# the block receiver when the "then" block is evaluated
case object
when Pattern { some nested[data] }.then do
puts some
puts data
end
|
|
I'm using Elixir's pattern matching also in function definitions, to do without ifs and switches. A trivial example with Elixir syntax (hopefully correct).
I'd like to have a pattern matching like that in Ruby as well. A more real world example: Much more readable than having to write ifs inside the function/method.All considered, I find Ruby a more pleasant language than Elixir because of syntax, verbosity, state keeping (probably unfair, because OO is made for that vs GenServers), deployment story. I'd like to keep using it for applications that can scale by adding processes, but pattern matching is a killer feature.
Pipelines are important in Elixir because it's functional. It would be a pain to code without them. Object oriented languages can somewhat pipeline by calling methods on results of previous methods, provided a consistent design of classes (each.map.uniq.sort). It seems that in Ruby it could remove lots of useless variables. A lot less head scratching to find sensible variable names, faster programming. I hope it gets into the language or that this gem gets popular.