From 8c89f3f6083c93fa7ca04148a0e7aa07ff0d4545 Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Mon, 29 Jul 2013 16:30:51 +0800 Subject: Explained syntactic sugar is really just method calls. Objects in ruby receive a message via the . (dot) notation. The arithmetic operators are just syntactic sugar of the . message notation. --- ruby.html.markdown | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/ruby.html.markdown b/ruby.html.markdown index 38d060a3..7729e0ff 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -312,4 +312,22 @@ dwight.name #=> "Dwight K. Schrute" # Call the class method Human.say("Hi") #=> "Hi" + + +=begin +Arithmetic is just syntactic sugar +for calling a method on an object: +=end +1.+ 1 #=> 2 +1.+(1) #=> 2 + +8.- 1 #=> 7 +8.-(1) #=> 7 + +10.* 2 #=> 20 +10.*(2) #=> 20 + +35./ 5 #=> 7 +35./(5) #=> 7 + ``` -- cgit v1.2.3 From 99e1c31b19547fbe117a77047bb23eff0edafaea Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Tue, 30 Jul 2013 15:26:51 +0800 Subject: Updated location, and reduced examples also added array [] access example. --- ruby.html.markdown | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/ruby.html.markdown b/ruby.html.markdown index 7729e0ff..9a59be90 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -4,6 +4,7 @@ filename: learnruby.rb contributors: - ["David Underwood", "http://theflyingdeveloper.com"] - ["Joel Walden", "http://joelwalden.net"] + - ["Luke Holder", "http://twitter.com/lukeholder"] --- ```ruby @@ -30,6 +31,11 @@ You shouldn't either 10 * 2 #=> 20 35 / 5 #=> 7 +## Arithmetic is just syntactic sugar +## for calling a method on an object +1.+(3) #=> 4 +10.* 5 #=> 50 + # Special values are objects nil # Nothing to see here true # truth @@ -121,6 +127,12 @@ array = [1, "hello", false] #=> => [1, "hello", false] array[0] #=> 1 array[12] #=> nil +# Like arithmetic, [var] access +# is just syntactic sugar +# for calling a method [] on an object +array.[] 0 #=> 1 +array.[] 12 #=> nil + # From the end array[-1] #=> 5 @@ -313,21 +325,4 @@ dwight.name #=> "Dwight K. Schrute" # Call the class method Human.say("Hi") #=> "Hi" - -=begin -Arithmetic is just syntactic sugar -for calling a method on an object: -=end -1.+ 1 #=> 2 -1.+(1) #=> 2 - -8.- 1 #=> 7 -8.-(1) #=> 7 - -10.* 2 #=> 20 -10.*(2) #=> 20 - -35./ 5 #=> 7 -35./(5) #=> 7 - ``` -- cgit v1.2.3 From df90e4957246c422c00b07f788d825d327304d11 Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Tue, 30 Jul 2013 15:28:40 +0800 Subject: Fixed comment style --- ruby.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ruby.html.markdown b/ruby.html.markdown index 9a59be90..a3bcbbd5 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -31,8 +31,8 @@ You shouldn't either 10 * 2 #=> 20 35 / 5 #=> 7 -## Arithmetic is just syntactic sugar -## for calling a method on an object +# Arithmetic is just syntactic sugar +# for calling a method on an object 1.+(3) #=> 4 10.* 5 #=> 50 -- cgit v1.2.3