diff options
author | Mark Green <mark@antelope.nildram.co.uk> | 2016-08-14 19:39:07 +0100 |
---|---|---|
committer | Mark Green <mark@antelope.nildram.co.uk> | 2016-08-14 19:39:07 +0100 |
commit | 62102d02992f83b3a1fb745a39f36332dd4435b7 (patch) | |
tree | d5ac79ceac2f61e5658aaa826304d87240a20110 /ruby.html.markdown | |
parent | 57053bc95d4166a42da8f6d1732dd21a217b073a (diff) | |
parent | 476de3289df9ef5f72330f3d1359ce9f93d71754 (diff) |
Merge remote-tracking branch 'refs/remotes/adambard/master'
Diffstat (limited to 'ruby.html.markdown')
-rw-r--r-- | ruby.html.markdown | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/ruby.html.markdown b/ruby.html.markdown index 3eed2d3c..adf5ce81 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -285,7 +285,7 @@ hash.each do |key, value| puts "#{key} is #{value}" end -# If you still need and index you can use "each_with_index" and define an index +# If you still need an index you can use "each_with_index" and define an index # variable array.each_with_index do |element, index| puts "#{element} is number #{index} in the array" @@ -411,6 +411,28 @@ def guests(*array) array.each { |guest| puts guest } end +# If a method returns an array, you can use destructuring assignment +def foods + ['pancake', 'sandwich', 'quesadilla'] +end +breakfast, lunch, dinner = foods +breakfast #=> 'pancake' +dinner #=> 'quesadilla' + +# By convention, all methods that return booleans end with a question mark +5.even? # false +5.odd? # true + +# And if a method ends with an exclamation mark, it does something destructive +# like mutate the receiver. Many methods have a ! version to make a change, and +# a non-! version to just return a new changed version +company_name = "Dunder Mifflin" +company_name.upcase #=> "DUNDER MIFFLIN" +company_name #=> "Dunder Mifflin" +company_name.upcase! # we're mutating company_name this time! +company_name #=> "DUNDER MIFFLIN" + + # Define a class with the class keyword class Human |