Ruby array
Today I've Learned postHere is some list of Ruby Array operations and examples.
I’m just pasting here some cool examples for lates Ruby I use. Pls check full list of Array features for your Ruby version
detect Same element in two Ruby arrays
Ruby
&
method docs
['a', 'b', 'c', 'd', 'e'] & ['a', 'c']
# => ["a", "c"]
['a', 'c'] & ['a', 'b', 'c', 'd', 'e']
# => ["a", "c"]
['a', 'b', 'c', 'd', 'e'].&(['a', 'c'])
will also work
Symbols and strings are not the same!
['a', 'b', :c, :d, :e] & ['a', 'c', :e]
# => ["a", :e]
['a', 'c', :e] & ['a', 'b', :c, :d, :e]
# => ["a", :e]
different element in two Ruby arrays
Ruby
difference
method docs
['a', 'b', 'c', 'd', 'e'].difference ['a', 'c']
# => ["b", "d", "e"]
['a', 'c'].difference ['a', 'b', 'c', 'd', 'e']
# => []
string and symbols:
['a', 'b', :c, :d, :e].difference ['a', 'c', :e]
# => ["b", :c, :d]
['a', 'c', :e].difference ['a', 'b', :c, :d, :e]
=> ["c"]
Entire blog website and all the articles can be forked from this Github Repo