I am new to programming in Ruby and have seen that it has some functional capabilities. Was wondering if there is a way to pattern match on arrays; I am looking to accomplish the following:
split_string = str.split("_", 2)
fst = repo_branch_split.first
snd = repo_branch_split.second
in a Haskell-like manner:
split_string@(fst : snd) = str.split("_", 2)
Is there anything similar in ruby or not?
This is a parallel assignment in Ruby. You can assign an array to variables this way:
fst, snd = str.split("_", 2)
You can also achieve head / tail behavior from Haskell by assigning rest of the array to a single variable:
head, *tail = "foo_bar_baz".split("_")
# head => "foo"
# tail => ["bar", "baz"]
Without star in tail
, it would assign only bar
string and baz
would "disappear".