get rubying
Programming Fundamentals
Programming
- programming is simply instructions for a computer to follow
- programming languages at their most basic level have 2 things: ways to store data, ways to manipulate data
- the order in which data is manipulated is called flow
- and the structures that determine that order are called control flow structures
A quick and dirty intro
We will cover:
- variables
- conditional statements
- loops
- functions
There is a LOT more to programming than the above.
where
-
irb: in terminal (in any folder), type irb
-
rails console: within a rails app folder, in terminal, type rails console
-
codecademylabs: labs.codecademy.com
- .rb files: create a .rb file and then type ruby <file.rb> in terminal
output
logging things to the console is a good way to check your variables.
in ruby, we log things by using the word puts
Example:
myVariable = 345
puts myVariable
>> 345
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
fizzbuzz
FizzBuzz is a classic interview question for developers:
Write a program that prints the numbers between 1-100
But for multiples of 3, print Fizz, instead of the number
and for multiples of 5 print Buzz
If the number is a multiple of 3 AND 5, print FizzBuzz
more problems
- Project Euler - math problems
- Ruby Koans - test driven way to learn ruby syntax
- Essential Phone Screen Questions - coding section
resources
functions
functions are way to store a block of code that you want to reuse
def functionName(parameters)
do something
end
you call a function by typing it's name:
funcitonName(params)
get rubying
By ag_dubs
get rubying
- 753