Variables
Variables "store" data.
Variables are ways to talk about data abstractly.
i.e.
3 + 2 = 5, or
x = 3
x + 2 = 5
VARiables
Variables have different types.
...some are familiar:
string = any set of characters
integer = whole numbers
Arrays
An array is a list of variables.
myArray = [variable1, variable2]
it can hold many types of variables or data
myVariable = 4
myArray = [myVariable, "hello", 38578]
you can even have arrays of arrays
Arrays
to get a value from an array you use this notation:
myArray[#], where # is the index of the array
example:
myArray = ['a','b','c', 'd', 'e', 'f']
myArray[0] #=> 'a'
myArray[4] #=> 'e'
Array practice
myArray = ['cat','dog','pig','horse','spider']
myArray[0]= ?
myArray.last = ?
myArray.first = ?
myArray[3] = ?
'dog' = ?
'spider' = ?
'horse' = ?
array functions
-
first/last returns first or last item in the array
-
length returns the length of the array
-
sort put the array in order (alphabetically or numerically)
-
reverse returns a new array in the reverse order
-
reverse! returns the same array in the reverse order
Array practice
myArray = ['cat','dog','pig','horse','spider']
myArray.sort = ?
myArray.sort! = ?
myArray.reverse = ?
myArray.reverse! = ?
myArray.length = ?
objects
Objects are just like Arrays except instead of numbers they have "keys" that have names (strings).
e.g.
myObject = { name: "Ashley",
age: 26,
email: "heyashleyashley@gmail.com" }
object practice
Create an object called 'myObject' for yourself that includes your name, age, and email address
conditional statements
We are going to want to be able to compare values. To do so we use conditional statements.
== are they equivalent?
>= greater than or equal
<= less than or equal
!= not equal
conditional statments
We can use conditional statements in structures to control how/if often a piece of code is run.
These are called control flow structures:
- if and unless statements
- for loops
- while loops
- case statements
if and unless
if (something) then
do this
else
do this
elsif
do this
end
do this if something
do this unless something
if and unless practice
if a number is greater than or equal to 5, subtract 1, otherwise add 2
if the word is 'hello' print it to the screen, otherwise print the word 'goodbye'
add 2 to a number unless that number = 10
for loops
for i in 1..5
do something
end
(1..5).each do |n|
puts n
end
for loop practice
- Print the number 1-100
- Print all the letters of the alphabet
- Create an array with 5 words and print them all out
- Create an array with 5 words and only print the words that start with the letter 's'
case statements
myARRAY = ["fruit", "meat", "dairy", "veggie"]
myFood = myARRAY[rand()*myARRAy.length]
case myFood
when "fruit"
do something
when "meat"
do something
end
Case practice
- Create an array with the words "colors", "shapes", "numbers"
- Create an array for each of those words with at least 3 elements in it
- Randomly select a word from your first array
- Write a case statement that will randomly select a word from the respective array
e.g. if colors if chosen, select a color from the colors array