1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-08-19 13:02:14 +02:00

Merge pull request #143 from lukeholder/patch-1

Explained syntactic sugar is really just method calls.
This commit is contained in:
Adam Bard
2013-07-31 23:25:15 -07:00

View File

@@ -4,6 +4,7 @@ filename: learnruby.rb
contributors: contributors:
- ["David Underwood", "http://theflyingdeveloper.com"] - ["David Underwood", "http://theflyingdeveloper.com"]
- ["Joel Walden", "http://joelwalden.net"] - ["Joel Walden", "http://joelwalden.net"]
- ["Luke Holder", "http://twitter.com/lukeholder"]
--- ---
```ruby ```ruby
@@ -30,6 +31,11 @@ You shouldn't either
10 * 2 #=> 20 10 * 2 #=> 20
35 / 5 #=> 7 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 # Special values are objects
nil # Nothing to see here nil # Nothing to see here
true # truth true # truth
@@ -121,6 +127,12 @@ array = [1, "hello", false] #=> => [1, "hello", false]
array[0] #=> 1 array[0] #=> 1
array[12] #=> nil 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 # From the end
array[-1] #=> 5 array[-1] #=> 5
@@ -312,4 +324,5 @@ dwight.name #=> "Dwight K. Schrute"
# Call the class method # Call the class method
Human.say("Hi") #=> "Hi" Human.say("Hi") #=> "Hi"
``` ```