2 ms·
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 mat
by tyingq 7y ago
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.
- rubyfan 7y agoYeah 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 is significantly different than `map` imo.
- s_kilk 7y agoMap is data manipulation, whereas pattern-matching is control flow.
- tyingq 7y agoYes, 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.