Last active
December 17, 2015 23:29
-
-
Save theotherzach/5689063 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
| # From Spotlight, search for terminal | |
| # From the terminal, type irb to start the Interactive Ruby shell | |
| # To leave irb, type exit | |
| # If shit gets weird, control-c | |
| ############################################ | |
| # Words for Mary | |
| # Special words | |
| # while, if, puts, do, end | |
| # most others are variables or numbers | |
| ############################################ | |
| # Variables | |
| a_number = 4 | |
| look_a_string = "string" | |
| i = 0 | |
| # Puts | |
| name = "Mary" | |
| puts name | |
| # Numbers and Math: | |
| 5 + 3 | |
| 3 * 2 | |
| dexter_weight = 15.5 | |
| dresden_weight = 14 | |
| annie_weight = 12 | |
| total_cat_weight = dexter_weight + dresden_weight + annie_weight | |
| number_of_cats = 3 | |
| average_cat_weight = total_cat_weight / number_of_cats | |
| puts average_cat_weight | |
| # Array and array.size | |
| cat_weights = [] | |
| cat_weights << 15.5 << 14 << 12 | |
| #=> [15.5, 14, 12] | |
| cat_weights[0] | |
| #=> 15.5 | |
| cat_weights[1] | |
| #=> 14 | |
| cat_weights.size | |
| #=> 3 | |
| # Loop | |
| i = 0 | |
| n = 10 | |
| while i < n | |
| i = i + 1 | |
| end | |
| puts i | |
| #=> 10 | |
| # Conditional | |
| number_of_cats = 3 | |
| if number_of_cats > 4 | |
| puts "holy shit you're nuts" | |
| elsif number_of_cats == 3 | |
| puts "you're pretty cool" | |
| else | |
| puts "lame!" | |
| 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
| # % is mod or remainder | |
| 12 % 5 | |
| #=> 2 | |
| 15 % 5 | |
| #=> 0 | |
| 15 % 10 | |
| #=> 5 | |
| 5 % 10 | |
| #=> 5 | |
| 100 % 10 | |
| #=> 0 | |
| 100 % 5 | |
| #=> 0 |
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
| # Loop with a conditional | |
| i = 0 | |
| n = 10 | |
| sum_of_even_i = 0 | |
| while i < n | |
| if i % 2 == 0 | |
| sum_of_even_i = i + sum_of_even_i | |
| end | |
| i = i + 1 | |
| end | |
| puts sum_of_even_i |
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
| # Conditionals with a loop | |
| cat_weights = [] | |
| cat_weights << 15.5 << 14 << 12 | |
| lightest_cat_weight = 0 | |
| i = 0 | |
| n = 3 | |
| while i < n | |
| if lightest_cat_weight == 0 || lightest_cat_weight < cat_weights[i] | |
| lightest_cat_weight = cat_weights[i] | |
| end | |
| i = i + 1 | |
| end | |
| puts lightest_cat_weight |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The cartoon/ visual way to learn ruby:
http://mislav.uniqpath.com/poignant-guide/