Created
January 16, 2019 11:12
-
-
Save rodloboz/a3b5360aff2307757fdd9bcd3fb0279e to your computer and use it in GitHub Desktop.
Ruby 04-Flow-Control Lecture
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
| # Data Types / Built-in Objects | |
| "Hello" # String | |
| 'Hello' # String | |
| 4 # Integer | |
| 4.3 # Float | |
| false # Boolean | |
| p [3, "Hello", 3.4, true] # Array | |
| # method | |
| first_name = "rui" # variable | |
| last_name = "freitas" | |
| # concatenation | |
| puts first_name.capitalize + " " + last_name.capitalize | |
| # interpolation | |
| puts "#{first_name.capitalize} #{last_name.capitalize}" | |
| # Methods | |
| def full_name(first_name, last_name) | |
| return "Hello, Beatrix!" if first_name == 'Beatrix' | |
| "#{first_name.capitalize} #{last_name.capitalize}" | |
| end | |
| p full_name('Beatrix', 'Kiddo') | |
| p full_name('Rui', 'Freitas') | |
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
| # puts "How old are you?" | |
| # print "> " | |
| # age = gets.chomp.to_i | |
| # IF structure | |
| # if age >= 18 | |
| # puts "You can vote!" | |
| # end | |
| # Inline if (same as above) | |
| # puts "You can vote!" if age >= 18 | |
| # puts "You can vote!" unless age < 18 | |
| # IF/ELSE structure | |
| if age >= 18 | |
| puts "You can vote!" | |
| else | |
| puts "Go home! You're a kiddo!" | |
| end | |
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
| puts "heads or tails?" | |
| print "> " | |
| # use input(string) | |
| guess = gets.chomp | |
| # randomized result(string) | |
| coin = ['heads', 'tails'].sample | |
| # if guess == coin | |
| # puts "Winner!" | |
| # else | |
| # puts "Loser!" | |
| # end | |
| # ternary operator | |
| result = guess == coin ? "Winner" : "Loser" | |
| puts "#{result}, that was #{coin}!" | |
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
| hour = Time.now.hour | |
| if hour < 12 | |
| puts "Good morning!" | |
| elsif hour >= 20 | |
| puts "Good night!" | |
| elsif hour >= 12 && hour != 13 | |
| puts "Good afternoon!" | |
| else | |
| puts "Enjoy your lunch!" | |
| end |
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
| puts "Which action? [read | write | list | dance | exit]" | |
| print "> " | |
| choice = gets.chomp.downcase | |
| case choice | |
| when "read" | |
| puts "You are in read mode" | |
| when "write" | |
| puts "You are in write mode" | |
| when "list" | |
| puts "You are in list mode" | |
| when "dance" | |
| puts "Dancingggg..." | |
| when "exit" | |
| puts "Goodbye" | |
| else | |
| puts "Incorrect input" | |
| puts "Plese try again" | |
| end | |
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
| # Bootcamp is open from 9 to 19 | |
| # But closes for lunch (13 to 14) | |
| hour = Time.now.hour | |
| # morning period | |
| def is_morning?(hour) | |
| (hour >= 9 && hour < 13) | |
| end | |
| # afternoon period | |
| def is_afternoon?(hour) | |
| (hour >= 14 && hour < 19) | |
| end | |
| condition = is_morning?(hour) || is_afternoon?(hour) | |
| if condition | |
| puts "We're open! Welcome!" | |
| else | |
| puts "Sorry, we're closed. Come back later!" | |
| end | |
| # Using ternary operator | |
| # condition ? do something : do something else | |
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
| price_to_find = rand(1..5) # integer | |
| choice = nil | |
| # p choice != price_to_find | |
| # while choice != price_to_find | |
| # puts "Guess the price?" | |
| # print "> " | |
| # # always give a chance to update condition | |
| # choice = gets.chomp.to_i # convert to integer | |
| # end | |
| # if / unless -> test condition, run once | |
| # while / until -> test condition, loop | |
| until choice == price_to_find | |
| puts "Guess the price?" | |
| print "> " | |
| # always give a chance to update condition | |
| choice = gets.chomp.to_i # convert to integer | |
| end | |
| puts "You won!! The price is #{price_to_find}" | |
| for i in (1..10) | |
| puts i | |
| end | |
| for i in ["a", "b", "c"] | |
| puts i | |
| end | |
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 | |
| # CRUD | |
| # Create | |
| arr = Array.new | |
| # arr = [] | |
| p arr.class | |
| arr2 = [4, 5, 6] | |
| p arr2 | |
| # Reading | |
| # 0 1 2 3 | |
| names = ["tango", "cash", "utah", "bodhi"] | |
| p names | |
| puts names[2] | |
| # Update | |
| names[2] = "johnny" | |
| p names | |
| names.last.capitalize! | |
| p names | |
| # Delete | |
| # delete by index | |
| names.delete_at(1) | |
| p names | |
| # delete by value | |
| numbers = [4, 5, 2, 67, 2, 5, 2, 355] | |
| p numbers | |
| numbers.delete(5) | |
| p numbers | |
| # Array methods | |
| # gets rid of duplications | |
| p numbers.uniq | |
| p names.sort! | |
| p names.reverse | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment