|
| 1 | +# Ruby 101 |
| 2 | + |
| 3 | +* Ruby — Programming language |
| 4 | +* Ruby on Rails — Web framework (think: Ruby's Django) |
| 5 | + |
| 6 | +## Syntax |
| 7 | + |
| 8 | +* Everything's an object. |
| 9 | +* `puts "something"` prints something. `puts("something")` is the equivalent (think: Python 2.x vs. Python 3.x) |
| 10 | +* Classes start with `def <NAME>(<INPUT_PARAMS>)` and end with... well, `end`. |
| 11 | +* Common practice in the Ruby community is to use two space indentation. |
| 12 | +* When needing to print a value of a variable, both `result = "Good night, #{name}"` and `result = "Good night, " + name` should do the trick. |
| 13 | +* There's a difference between using single quotes and double quotes. Single quotes have no processing, double quotes do (you can't use something like `#{name}` or `\n` with single quotes). |
| 14 | +* `a = [ 'ant', 'bee', 'cat', 'dog', 'elk' ]` == `a = %w{ ant bee cat dog elk }` |
| 15 | + |
| 16 | +### Names: |
| 17 | + |
| 18 | +* Local variables: `something` `kthxbye`, `_x`, `_26` |
| 19 | +* Instance variables: `@something` `@kthxbye`, `@x`, `@_` |
| 20 | +* Class variable: `@@something`, `@@x_pos`, `@@sum` |
| 21 | +* Global variable: `$debug`, `$plan9`, `$CUSTOMER`, `$_` |
| 22 | +* Class name: `Something`, `Kthxbye`, `String`, `ActiveRecord`, `MyClass` |
| 23 | +* Constant name: `FEET_PER_MILE`, `DEBUG` |
| 24 | +* `Null` -> `nil` (object that represents nothing) |
| 25 | + |
| 26 | +### Hashes (something like dicts) |
| 27 | + |
| 28 | +```ruby |
| 29 | +inst_section = { |
| 30 | + 'cello' => 'string', |
| 31 | + 'clarinet' => 'woodwind', |
| 32 | + 'drum' => 'percussion', |
| 33 | + 'oboe' => 'woodwind', |
| 34 | + 'trumpet' => 'brass', |
| 35 | + 'violin' => 'string' |
| 36 | +} |
| 37 | +p inst_section['oboe'] # -> Returns "woodwind" |
| 38 | +p inst_section['cello'] # -> Returns "string" |
| 39 | +p inst_section['bassoon'] # -> Returns nil |
| 40 | +``` |
| 41 | + |
| 42 | +`:name` symbol is used commonly to replace the usage of single quotes. So, the above could also be used as such: |
| 43 | + |
| 44 | +```ruby |
| 45 | +inst_section = { |
| 46 | + :cello => 'string', |
| 47 | + :clarinet => 'woodwind', |
| 48 | + :drum => 'percussion', |
| 49 | + :oboe => 'woodwind', |
| 50 | + :trumpet => 'brass', |
| 51 | + :violin => 'string' |
| 52 | +} |
| 53 | +inst_section[:oboe] # -> Returns "woodwind" |
| 54 | +inst_section[:cello] # => Returns "string" |
| 55 | +inst_section['cello'] # => Returns nil, because `:cello` is not equal to `'cello'` |
| 56 | +``` |
| 57 | + |
| 58 | +This is also a valid syntax: |
| 59 | + |
| 60 | +```ruby |
| 61 | +inst_section = { |
| 62 | + cello: 'string', |
| 63 | + clarinet: 'woodwind', |
| 64 | + drum: 'percussion', |
| 65 | + oboe: 'woodwind', |
| 66 | + trumpet: 'brass', |
| 67 | + violin: 'string' |
| 68 | +} |
| 69 | +puts "An oboe is a #{inst_section[:oboe]} instrument" |
| 70 | +# Outputs: An oboe is a woodwind instrument |
| 71 | +``` |
| 72 | + |
| 73 | +### Control Structures |
| 74 | + |
| 75 | +Instead of the braces for the `if` and `while` loops, Ruby uses the keyword `end` to mark the end of the loop. |
| 76 | + |
| 77 | +`if` example: |
| 78 | + |
| 79 | +```ruby |
| 80 | +today = Time.now |
| 81 | +if today.saturday? |
| 82 | + puts "Do chores around the house" |
| 83 | +elsif today.sunday? |
| 84 | + puts "Relax" |
| 85 | +else |
| 86 | + puts "Go to work" |
| 87 | +end |
| 88 | +``` |
| 89 | + |
| 90 | +`while` example: |
| 91 | + |
| 92 | +```ruby |
| 93 | +num_pallets = 0 |
| 94 | +weight = 0 |
| 95 | +while weight < 100 and num_pallets <= 5 |
| 96 | + pallet = next_pallet() |
| 97 | + weight += pallet.weight |
| 98 | + num_pallets += 1 |
| 99 | +end |
| 100 | +``` |
| 101 | + |
| 102 | +Ruby supports something called statement modifiers, so: |
| 103 | + |
| 104 | +```ruby |
| 105 | +if radiation > 3000 |
| 106 | + puts "Danger, Will Robinson" |
| 107 | +end |
| 108 | +``` |
| 109 | + |
| 110 | +...is equal to: |
| 111 | + |
| 112 | +```ruby |
| 113 | +puts "Danger, Will Robinson" if radiation > 3000 |
| 114 | +``` |
| 115 | + |
| 116 | +...and: |
| 117 | + |
| 118 | +```ruby |
| 119 | +square = 4 |
| 120 | +while square < 1000 |
| 121 | + square = square*square |
| 122 | +end |
| 123 | +``` |
| 124 | + |
| 125 | +...is equal to: |
| 126 | + |
| 127 | +```ruby |
| 128 | +square = 4 |
| 129 | +square = square*square while square < 1000 |
| 130 | +``` |
| 131 | + |
| 132 | +### Manipulating strings |
| 133 | + |
| 134 | +Ruby supports regex out of the box with the "match" parameter, so this is legit: |
| 135 | + |
| 136 | +```ruby |
| 137 | +line = gets |
| 138 | +if line =~ /Perl|Python/ |
| 139 | + puts "Scripting language mentioned: #{line}" |
| 140 | +end |
| 141 | +``` |
| 142 | + |
| 143 | +...and basically searches for `Perl` *or* `Python` to satisfy the criteria. |
| 144 | + |
| 145 | +* `newline = line.sub(/Perl/, 'Ruby')` will replace the first occurrence of `Perl` with `Ruby`. |
| 146 | +* `newerline = newline.gsub(/Python/, 'Ruby')` will replace _all_ occurrences of the word `Python` with `Ruby`. |
| 147 | +* `newset = line.gsub(/Perl|Python/, 'Ruby')` will replace _all_ occurences of the word `Perl` _and_ the word `Python` with `Ruby`. |
0 commit comments