Skip to content

Instantly share code, notes, and snippets.

@lmsilvera
Last active September 16, 2021 17:05
Show Gist options
  • Select an option

  • Save lmsilvera/cdf6f79616773d1b46a4fe8549e467b9 to your computer and use it in GitHub Desktop.

Select an option

Save lmsilvera/cdf6f79616773d1b46a4fe8549e467b9 to your computer and use it in GitHub Desktop.
Array and Hashes Workshop notes.
# Ruby 2.7.0
ar = [1,2,3,4]
ar.first
ar.last
ar[1]
ar << 5
ar.push(6)
ar.map(&:to_s)
ar.delete(3)
ar.delete_at(2) # use index
ar.unique
ar.select{|n| n % 2 == 0}
ar.reject{|n| n % 2 == 0}
ar.include?(4)
ar.sort
puts ar.map{|n| n * 2 }
arr.each{|n| puts n * 2 }
arr.size
arr.count
arr.any?
arr.empty?
arr.compact
arr.flatten
arr.sum # result: 21
arr.reverse
arr.sample
arr.slice
# Ruby 2.7
ar2 = [1,1,2]
ar2.map(&:even?).tally
# bang !
h = {a: 1, b: 2, c: 2}
h.key?(:a)
h[:a]
h.to_a
h.keys
h.values
h.size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment