#WDI Day 2
JSConf AU - Melbourne April
"You never really understand Mahtematics, you just get used to it.
####Flow control:
If conditions Loops Calc it
type ruby in the terminal to load the file.
echo $PATH looks in all the folders of your computer where Terminal looks to find programs.
which : which file is going to run. Example: which ruby finds the first program called ruby the terminal can find. Helps find where a program is.
constants are all uppercase
irb Process.pid give syou the PID of the probram ruby
ruby + name of file : gives you the PID of the file.
!= pronouced bang
CTRL R : reverse search of what we've been doing before and type some of the files name. Saves time
Dry code - Don't repeat yourself
case statement - avoids repeating a variable name.
Example:
brother = "Harpo" case brother when "Groucho" puts "Cigar" when"Chico" puts "Piano" when"Harpo" puts "Harp" end
while loops are infite loops
Read things from the user: "gets" : get string, but includes new line after input
Command and forward slash - put commands as comments. To undo re-do command + /
gets.chomp : remove the new line at the end of the user input.
Joel's code from the course (https://github.com/wofockham/wdi-4)
####Functions
Javascript
functions are what's called first-class
subl . opens all files in Sublime
def defines a function.
def area (length, width) --> placeholders or arguments to the function a = length * width a end
puts "The area is #{ area(12,5)}" --> returns 60
placeholders can include variables bathroom_area = area(bathroom_length * bathroom_width)
CTRL-l to clear screen
see docs function.rb and calcit.rb
CTRL-c quits process
###Add a file on your Github depository
- git add
- git commit - "comment"
- git push
(https://github.com/wofockham/wdi-4/blob/master/01-ruby/calcit.rb)
Problem: Calculator
sqrt - square root exponentiation - a ** b (a to the power of b) odd or even numbers
Kernel.exit or just 'exit'--> exit program
A string is considered true. if "" --> considered true even if nothing in string.
The number 0 is considered true in Ruby
An array is also true even if empty
nil is considered false.
Index starts from 0. inex[-1] starts from the end.
we can overwrite values in an array and add values by doing a[new index] = 'string'
if we havent assigned something specifically in an array, we get a nil back.
[a].length / .size / .count = gives you the size of the array
be consistent with the one you use.
a[start, length] will ask the array to start at position 'start' and will count the amount of values 'length'
####Methods
a.push --> pushed something onto the array. Adds a value at the end of the array
a.pop --> removes the last value from the array.
a.shift --> removes the first element of the array
a.unshift --> puts an element at the front of the array
a.reverse --> switch the elements of the array but if we display the array again, the order is the same as previous one cause it created a copy of the array.
a.reverse! --> actually reverses the order of the array as it makes the change in the original array.
We can avoid writing all the quotes symbols by using .split' '. Just write 'Groucho Harpo Zeppo'.split' ' and it gives ["Groucho", "Harpo", "chico", "Zeppo"]
r.first == r[0]
the principles of no surprise
r.last
Concatenate operator << to append elements to array at the end
r.delete_at index --> removes the element at the index 2
r.sample --> gives a random element of an array
chapters.index("element") gives you the position of the element you are looking for in the array.
chapters = ["An unexpected party", "Roast Mutton", "A short rest"] chapters [1] --> "A Roast Mutton" chapters.index("Roast Mutton") --> 1
We can have arrays nested in arrays
n = [1,2,8,["foo","bar"],7,true] n[3] => ["foo", "bar"] n[3][1] => "bar"
n.flatten : returns a flatten value of the array but did not modify the original array. n.flatten! changes the original array
a.uniq : gives the values that are unique in the array if some values are repeated. a.uniq.size : gives you the size of the unique values of the array
####Homework:
Guessing game - see gist
Calcit:
Body Mass Calculator Mortgage calculator