summaryrefslogtreecommitdiffhomepage
path: root/ruby.html.markdown
diff options
context:
space:
mode:
authorRyan Plant <ryan@ryanplant.net>2016-01-26 11:58:46 +1100
committerRyan Plant <ryan@ryanplant.net>2016-01-26 11:58:46 +1100
commitb9c1502cab19360bda496df5a2503a198f7c4f50 (patch)
tree442a3a6eb4161f06e311d2f757fe51e209ccf9e9 /ruby.html.markdown
parent0f5c74a79f328aa1b385a2dae3389d48faa47ce5 (diff)
Add note on method naming conventions
Diffstat (limited to 'ruby.html.markdown')
-rw-r--r--ruby.html.markdown13
1 files changed, 13 insertions, 0 deletions
diff --git a/ruby.html.markdown b/ruby.html.markdown
index 6743de6b..bdad4d06 100644
--- a/ruby.html.markdown
+++ b/ruby.html.markdown
@@ -419,6 +419,19 @@ 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