- Symbols' values remain the same / constant (Technical Term: 'Immutable', we can say symbols are immutable).
- Multiple uses of the same symbol have uses the same amount of memory (no new copies, single copy per symbol)
- (For Future Ref. // Not discussed yet) You can't call any of the String methods like #upcase, #split on Symbols.
Created
June 30, 2016 23:32
-
-
Save FaisalAl-Tameemi/3fee1ff702d944ccd8cd2c5eb6aec5ad to your computer and use it in GitHub Desktop.
Staff Learn To Code // Chapter #1 - The Basics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# NOTE: | |
# for basic data structures such as strings, boolean and integers, | |
# please have a look at week 1 day 2 lecture notes | |
#################################################################### | |
# ARRAYS: lists of things | |
# an array of names | |
names = ["Faisal", "Sara", "Jane"] | |
# an array of arrays | |
xy_data_points = [[1, 2], [2, 3], [3,4]] | |
# an array of mixed data types | |
mixed = [true, 45, "just a string"] | |
#################################################################### | |
# HASHES: key, value pairs | |
# hashes are great for representing objects that have attributes (referred to as `keys`) that have values | |
person_1 = { | |
name: "Jane Doe", | |
age: 32 | |
} | |
# Each key (attribute) of a hash has a value which could be any data type | |
person_2 = { | |
name: "John Doe", | |
age: 24, | |
hobbies: ["Tennis", "Soccer", "Basketball"] # notice how the value here is an array | |
} | |
person_3 = { | |
name: "John Doe", | |
age: 24, | |
meta_data: { # notice how the value is a hash | |
data_of_birth: "1992-01-01" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#################################################################### | |
# ARRAYS | |
# let's say we have an array of names (where each name is a string) | |
names = ["Faisal", "Sara", "Jane"] | |
# arrays are 0 indexed, which means the index of the first item is 0 | |
puts names[1] # prints "Sara" to the terminal | |
# in ruby, trying to access an array position that doesn't exist return nil | |
# may give you an error in other languages | |
puts names[500] # prints nil or nothing to the terminal | |
# to get the last item of an array without knowing the length | |
puts names[-1] # using -1 as index with automatically map to the last index in the array | |
# arrays come with methods defined by ruby out of the box | |
puts names.length # returns the number of items in an array | |
#################################################################### | |
# HASHES | |
# let's say we have a hash to represent a person | |
john = { | |
name: "John Doe", | |
age: 24, | |
hobbies: ["Tennis", "Soccer", "Basketball"] | |
} | |
# if we need to access a specific property of a hash, we do it as follows | |
puts john[:name] # prints "John Doe" to the terminal | |
puts john[:hobbies] # prints the list of hobbies inside of our object | |
# we can also chain our accessors, ex: getting the first hobby in the hobbies list | |
puts john[:hobbies][0] # prints "Tennis" to the terminal |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#################################################################### | |
# STORING DATA IN ARRAYS | |
# let's say we have an array of names (where each name is a string) | |
names = ["Faisal", "Sara", "Jane"] | |
# if we want to add more names to our array, we do it as follows: | |
names << "Vicky" | |
names << "Norman" | |
puts names # outputs a list of 5 names (including the 2 we added) | |
#################################################################### | |
# STORING DATA IN HASHES | |
# let's say we have a hash to represent a person | |
jane = { | |
name: "Jane Doe", | |
age: 32 | |
} | |
# we can add another attribute to this hash, ex: if we want to add hobbies to this person | |
jane[:hobbies] = ["Basketball", "Painting"] | |
jane[:favourite_fruit] = "Orange" | |
puts jane # prints the jane object along with the new attributes (keys) we added along with their values |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment