mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-05 06:17:36 +02:00
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.
This commit is contained in:
@@ -312,4 +312,22 @@ dwight.name #=> "Dwight K. Schrute"
|
|||||||
|
|
||||||
# Call the class method
|
# Call the class method
|
||||||
Human.say("Hi") #=> "Hi"
|
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
|
||||||
|
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user