Created
February 28, 2020 03:29
-
-
Save harrisonmalone/216f2a2f2bacfe711903733d089c3c6d to your computer and use it in GitHub Desktop.
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
| # everything in ruby is truthy apart from nil and false | |
| # truthy | |
| "string" | |
| 1 | |
| 1.0 | |
| ["hi"] | |
| {hi: "hello"} | |
| # falsy | |
| nil | |
| false | |
| # to test if something is truthy or falsy | |
| !!"string" | |
| # an example of where you'd have a nil value in your if statement | |
| database = [ | |
| {name: "harrison"}, | |
| {name: "ed"} | |
| ] | |
| found_user = database.find do |user| | |
| user[:name] == "nic" | |
| end | |
| if !found_user | |
| puts "this is a new user" | |
| else | |
| puts "the user exists" | |
| end | |
| # ternary operator | |
| bright = true | |
| house_lights_message = bright ? "Turn down the lights" : "It's very dark in here" | |
| # methods | |
| def burger_menu | |
| puts "welcome to burger joint ๐" | |
| puts "" | |
| puts "heres the menu" | |
| puts "1. cheeseburger" | |
| puts "2. vegan burger" | |
| puts "3. chicken burger" | |
| puts "4. ultimate double whopper with fries" | |
| puts "" | |
| end | |
| def get_user_input | |
| puts "please place an order" | |
| print "> " | |
| user_selection = gets.chomp.to_i | |
| return user_selection | |
| end | |
| def burger_selection(user_selection) | |
| case user_selection | |
| when 1 | |
| puts "you ordered a cheeseburger" | |
| when 2 | |
| puts "you ordered a vegan burger" | |
| when 3 | |
| puts "you ordered a chicken burger ๐" | |
| when 4 | |
| puts "you ordered a ultimate double whopper with fries" | |
| else | |
| puts "wrong user input" | |
| end | |
| end | |
| # calling 3 methods | |
| burger_menu() | |
| user_selection = get_user_input() | |
| burger_selection(user_selection) | |
| # scope | |
| # what is scope? | |
| # file scope, global scope, scope outside of methods | |
| # method scope, scope inside of methods | |
| # one approach you can take to handling scoping issue is passing arguments to methods | |
| # this variable is in the global file scope | |
| my_variable = "hi" | |
| def sum | |
| # this variable is in the method scope | |
| another_variable = "sup" | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment