#Arrays# ###Using the Docs###
[1,2,3,4].each {|x| puts x + 1 }
2
3
4
5
=> [1,2,3,4]
Some methods are aliased map => collect collect => map
Some methods take in optional arguements a = [11,12,13,14,15] puts a.first => 11
puts a.first(3)
=> [11,12,13]
Some Favorite Array methods!
- each
- map
- map!
- reject
- select
- detect
- & (intersection)
- [+] (concatenation)
- << (insertion)
- flatten
- uniq
- compact
- first
- last
- count
- each_with_object
a = ["a","s","c","d","d"]
a.count
=> 5
a.count {|x| x == "d"}
=> 2
Some might look alike, but some may have the "!" operator at the end. ! is called a bang in programming
a = ["a","b","c","b"]
b = a.map {|x| "foo:" + x }
puts b.inspect
=> ["foo:a", "foo:b", "foo:c", "foo:b"]
puts a.inspect
=> ["a","b","c","b"]
a hasn't changed at all, but if you wanted to change it, you could use map! to modify the original
b = a.map! {|x| x + "foo"}
puts a.inspect
=> ["foo:a", "foo:b", "foo:c", "foo:b"]