Created
April 5, 2014 19:43
-
-
Save JoseJRVazquez/9997109 to your computer and use it in GitHub Desktop.
This file contains 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
SO sometimes we have to as a question, and have a result based on that answer. For example | |
"I Mary doesnt have two shoes on, ask her 'why'" | |
For assignment is the following quesiton is given | |
"If Mary has more than $5, then give her an apple." | |
So the following code is given as an example | |
# define the method: Here they are defining the method that evaluates if mary can have an apple | |
def can_buy_apple_with?(money) | |
if money > 5 | |
"have an apple" | |
end #if statements must be closed with an end also before closing the method | |
end | |
# call the method, passing in a 7, and the method will answer with "have an apple" | |
can_buy_apple_with?(7) | |
#=> "have an apple" | |
# call the method, passing in a 5, and the method will answer with "nil" | |
can_buy_apple_with?(5) | |
#=> nil | |
Since the we need a way to answer either way, the following ELSE statement is added to the method | |
def can_buy_apple_with?(money) | |
if money > 5 | |
"have an apple" | |
else | |
"sorry, how about some gum?" | |
end | |
end | |
We call the method two ways | |
# call the method, passing in a 7 | |
can_buy_apple_with?(7) | |
#=> "have an apple" | |
# call the method, passing in a 5 | |
can_buy_apple_with?(5) | |
#=> "sorry, how about some gum?" | |
i tried this in the editor and it worked well | |
the question is, what to do if the amount 5 == 5, which is where ELSEIF comes into play | |
So the following change in the program is suggested | |
def can_buy_apple_with?(money) | |
if money > 1_000 | |
"have an Apple computer" | |
elsif money > 5 | |
"have an apple" | |
elsif money == 5 | |
"have some gum" | |
else | |
"get out of my store" | |
end | |
end | |
# call the method, passing in a 2_000 | |
can_buy_apple_with?(2_000) | |
#=> "have an Apple computer" | |
# call the method, passing in a 6 | |
can_buy_apple_with?(6) | |
#=> "have an apple" | |
# call the method, passing in a 5 | |
can_buy_apple_with?(5) | |
#=> "have some gum" | |
# call the method, passing in a 4 | |
can_buy_apple_with?(4) | |
#=> "get out of my store" | |
Note to Oliver: I spent an hour plpaying with this in IRB and got it wrong most of the time and then finally realized why we need RSPEC....Damn TYPOS! | |
but I digress | |
Unless and ! | |
apparently Unless is used for simple logic and comparison. the example given is as follows: | |
def can_buy_apple_with?(money) | |
unless money < 1_000 | |
"have an Apple computer" | |
else | |
"have an apple" | |
end | |
end | |
Now you can refactor the code to us if statments, but to make it simpler but get the same effect, you can use ! | |
def can_buy_apple_with?(money) | |
if !(money < 1_000) | |
"have an Apple computer" | |
else | |
"have an apple" | |
end | |
end | |
basically, the ! flops the normal logic of an if statement from "if it does" to "if does not". | |
One Line Conditionals | |
So apparently ruby lets you do small one-line conditions, such as the following; | |
gets_discount = true | |
price *= 0.8 if gets_discount | |
skip_tax = true | |
price += price * 0.1 unless skip_tax | |
If gets_discount is true (which it is) then you'll discount the price by 20%. Unless skip_tax is true, you'll add a 10% tag to the price. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
adding statements of AND and OR, AKA && and | |
if statements simply look for a true or false value. To be clear, true and false values do not have to be boolean values. In Ruby, nil and false are all considered to be "false-y" values. Everything else in Ruby is naturally true. Consider the following example: