Week 2: Makers Academy

Week 2: Makers Academy

·

5 min read

This week was primarily focusing on continuing learning Ruby and going through solving quizzes based on our learning.

Arrays

Arrays are indexed collection of related items that can store many other objects inside.

Many ways to create arrays

array = Array.new
# []
array = [1, 'two', 3]
Array.new(3, 1)
# [1,1,1]

Modify arrays

  • Use .push method to add strings into the array
  • Use .pop method to remove the last element of an array
  • Use .delete_at() method with an argument indicating the index of the element we want to remove

Access elements in the array

Square bracket notation

We can use the square bracket notation to read an element in the array.

array = [1,2,3,4,5,6,7,8,9]
array[3]
# 4

Array methods

  • .slice(0,1): takes two arguments, the first argument is the starting index, the last argument is the length of the slice.
  • .join(" "): here we used a space, the array of strings will concatenate them together to form one string.
  • .first: returns the first element in the array
  • .include?: returns true if the given object does exist in the array, otherwise returns false.
  • .select: returns a new array containing all elements of the array where the provided block returns true.
  • .map: returns a new array containing the values returned from the block and carries out the given block once for each element in the array.
  • .each: returns the array itself and calls the given block once for each element.

Strings store lists just like how array stores a list of elements, so can call some array methods on strings.

Multi-dimensional Arrays

multi_array = Array.new(3) {Array.new()}
# [[], [],[]]

To access a multi-dimensional array use square bracket notation

array = [[1,2,3,4],
         [5,6,7,8]]
array[0][1]
# 2

Hashes

Hashes and arrays are similar in that they both contain a list of elements. The difference is that:

  • Array elements are objects
  • Hash elements are two objects, one to used to reference the other.

Hashes store key-value pairs and can access values by their keys. They can contain symbols which are immutable.

Create a new Hash using Hash.new

person = Hash.new()
person[:name] = 'bob'
person[:age] = 99
person[:height] = 200
p person

Can also put a default return value when the key does not exist in the hash. Ex. Hash.new("not yet set")
If I try to access a key that doesn't exist like person[:profession], it will return 'not set yet'.

Create a new Hash using a hash literal

another_person = {}
another_person[:name] = 'polly'
another_person[:age] = 100
another_person[:height] = 201

Commonly use symbols than strings as keys. Using strings as keys does work but can become messy as you cannot access strings using symbols. Symbols are immutable so cannot be changed once they're set. We rarely want to change keys in a hash so it's a suitable choice.

# person[:hometown] // nil
person["hometown"] = "london"

Create another hash as a value inside a hash

another_person[:partner] = person 

# another_person
# {:name=>"polly", 
#  :age=>100, 
# :height=>201, 
# :partner=>{:name=>"bob", :age=>99, :height=>200, "hometown"=>"london"}}

Access person by another_person[:partner][:name] - returns bob, accesses the nested hash and retrieves the partner and then into name.

Fun fact! Favourite_things[:sport] is exactly the same as favourite_things[:"sport"] It is syntactic sugar so there is no need to add quotes around the symbol.

Hashes allow alternate syntax for keys that are symbols:

options = { :font-size => 10, :font_family => "Arial" }

# SAME AS 

options = { font-size: 10, font_family: "Arial" }