#WDI – DAY TWO
##RUBY
###RESOURCES www.ruby-doc.org Great documentation for Ruby
http://www.codecademy.com/glossary/ruby#arrays
Ruby notes from Simon: https://gist.github.com/nizmox/9198327
http://www.ruby-doc.org/core-2.1.1/Array.html
http://www.ruby-doc.org/core-2.1.1/Array.html#method-i-sort
Types: Strings, integers, floats, Booleans Collections: Arrays Variables Assignment
Looking at today: Flow control, if conditions, loops
Ruby – v = to see which version of Ruby you are running
Constants are always uppercase != - not equal to ! is bang in computer language
An if statement…. if something puts "something is true" end
if 15 < 7 puts "15 is indeed less than 7" end
if ! (15 < 7) puts "15 is not less than 7" end if 15 < 7 puts "15 is indeed less than 7" (IF TRUE) else puts "15 is not less than 7" (IF FALSE) end
if 15 < 7 puts "15 is indeed less than 7" elsif 15 < 20 puts "15 is less than 20" else puts "something else is going on" end
score = 75 if score >= 80 puts "A Grade" elsif score >= 70 puts "B Grade" elsif score >= 60 puts "C Grade" else puts "F" end
FLOW CONTROL Code that flows from top to bottom
Ctl + R = (reverse-i-search)`': ….. an easier way to find and run a command that you previously typed in if true puts "That thing is true" end
while true (Infinite loop – don’t do it!) puts "This is also true" end Ctrl + C will stop it within Terminal Cmnd + / = comments a block of text .chomp = removes the last line character 8.4.round = rounds it to the nearest number
Print for same line input Puts for next line input
puts "You are old" if age > 50 = reversed if statement
Ruby is good for functions and functions are first class F(x) = x2 (squared) eg, square(8) or square 8 will equal 64 Subl . – will open all your Sublime files and show a handy menu on the left listing all files in the folder
Command + B = in Sublime Text, works out a function in an .rb file (equations) Control + L = clear irb screen in terminal under ruby
Paste in Ruby code in terminal and then run the function to test (rather than using the .rb file each time)
( ) = calling the function
gets.chomp = for strings gets.chomp.to_i = for integers (numbers)
For advanced calculator: sgrt – square root exponentiation eg, a ** b BMI Mortgage calculator
expressions => values 23 * 2 => 46
ONE SHOT if _________ do_something end
do_something if________
LOOPS while____________ do_something end
ARRAYS A string is considered true If [ ] ← an empty array if { } ← an expression a.length……..will give you the length of the array a.push “adds this text at the end of an array” a.pop - pulls the last bit of text from the end of an array a.shift - pulls the first bit of text from the start of an array a.unshift - undo above a.reverse a.delete_at - variable.delete_at(index) eg, fruit.delete_at(5) will delete the fifth fruit in the list of fruits a.sort - put list in alphabetical order a.insert – (index, object to insert) eg, (0, “Mango”) will put Mango at the front of the list because the index is 0 http://gistpages.com/2013/07/28/ruby_arrays_insert_append_length_index_remove
a.flatten - flattens arrays within arrays so it is just one a.sample - outputs random array choices
2.0.0-p353 :032 > a => ["harpo", "groucho", "chico"] 2.0.0-p353 :033 > a.reverse => ["chico", "groucho", "harpo"] 2.0.0-p353 :034 > backwards_bros = a.reverse => ["chico", "groucho", "harpo"] 2.0.0-p353 :035 > a => ["harpo", "groucho", "chico"] 2.0.0-p353 :036 > backwards_bros => ["chico", "groucho", "harpo"] 2.0.0-p353 :037 > a => ["harpo", "groucho", "chico"] 2.0.0-p353 :038 > a.reverse! => ["chico", "groucho", "harpo"] 2.0.0-p353 :039 > a => ["chico", "groucho", "harpo"] 2.0.0-p353 :040 > backwards_bros => ["chico", "groucho", "harpo"] 2.0.0-p353 :041 >
To add in commas in a list 2.0.0-p353 :041 > "Groucho Harpo Chico Zeppo" => "Groucho Harpo Chico Zeppo" 2.0.0-p353 :042 > "Groucho Harpo Chico Zeppo" .split ‘ ‘ => ["Groucho", "Harpo", "Chico", "Zeppo"]
2.0.0-p353 :044 > "Groucho Mrx:Harpo Mrx:Chico Mrx:Zeppo Mrx".split ':' => ["Groucho Mrx", "Harpo Mrx", "Chico Mrx", "Zeppo Mrx"] 2.0.0-p353 :045 >
<< adds you can have arrays within arrays
Repeats within an array 2.0.0-p353 :005 > a = ["Bob", "Bob", "Betty", "Bob", "Jim"] => ["Bob", "Bob", "Betty", "Bob", "Jim"] 2.0.0-p353 :006 > a => ["Bob", "Bob", "Betty", "Bob", "Jim"] 2.0.0-p353 :007 > a.uniq => ["Bob", "Betty", "Jim"] 2.0.0-p353 :008 > a.size => 5 2.0.0-p353 :009 > a.uniq.size => 3
HOMEWORK For advanced calculator: sqrt – square root exponentiation eg, a ** b BMI (http://www.wellho.net/resources/ex.php4?item=r111/bmi.rb) Mortgage calculator (http://www.ktechsystems.com/calculating-a-mortgage-payment-and-future-balance-using-ruby/)
RESOURCES FOR CREATING CALCULATORS IN RUBY http://www.youtube.com/watch?v=WJBt9CBA37I Source files: http://philipk.ca/download/Rubymath.zip
Guessing game: Best resource I found for this exercise…. http://www.dreamsyssoft.com/ruby-scripting-tutorial/loops-tutorial.php
odd and even http://www.natontesting.com/2011/01/01/rubys-each-select-and-reject-methods/
MY TUMBLR! http://an-hmnn.tumblr.com/