summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDavid Underwood <david.underwood@jadedpixel.com>2013-07-01 12:27:15 -0400
committerDavid Underwood <david.underwood@jadedpixel.com>2013-07-01 12:27:15 -0400
commit07aa1d9337252eea9f4d5103e028c894132be91f (patch)
treecf29a37eaafb75f91c672ec11c1c37dd026a9d71
parentb9e0c189ee856de31a3fd035b39bd54765104a8d (diff)
Adds sections for symbols, hashes, and yields
-rw-r--r--ruby.html.markdown51
1 files changed, 50 insertions, 1 deletions
diff --git a/ruby.html.markdown b/ruby.html.markdown
index a317c498..b07c92c9 100644
--- a/ruby.html.markdown
+++ b/ruby.html.markdown
@@ -72,6 +72,18 @@ snake_case = true
path_to_project_root = '/good/name/'
path = '/bad/name/'
+# Symbols
+# Symbols are immutable, reusable constants represented internally by an integer value
+# They're often used instead of strings to efficiently convey specific, meaningful values
+
+status = :pending
+
+status == :pending #=> true
+
+status == 'pending' #=> false
+
+position = :left
+
# Arrays
# This is an array
@@ -115,6 +127,33 @@ array.pop #=> [1, 2, 3, 4, 5, 6]
# Note that push and pop do the opposite of each other
# Shift and unshift are the same.
+# Hashes are Ruby's primary dictionary with keys/value pairs.
+# Hashes are denoted with curly braces:
+hash = {'color' => 'green', 'number' => 5}
+
+hash.keys #=> ['color', 'number']
+
+# Hashes can be quickly looked up by key:
+hash['color'] #=> 'green'
+hash['number'] #=> 5
+
+# Asking a hash for a key that doesn't exist returns nil:
+hash['nothing here'] #=> nil
+
+# Iterate over hashes with the #each method:
+hash.each do |k, v|
+ puts "#{k} is #{v}"
+end
+
+# Since Ruby 1.9, there's a special syntax when using symbols as keys:
+
+new_hash = { defcon: 3, action: true}
+
+new_hash.keys #=> [:defcon, :action]
+
+# Tip: Both Arrays and Hashes are Enumerable
+# This means they share a lot of useful methods
+
# Control structures
if true
@@ -160,7 +199,7 @@ end
grade = 'B'
case grade
when 'A'
- puts "way to go kiddo"
+ puts "Way to go kiddo"
when 'B'
puts "Better luck next time"
when 'C'
@@ -198,5 +237,15 @@ sum sum(3,4), 5 #=> 12
# All methods have an implicit, optional block parameter
# it can be called with the 'yield' keyword
+def surround
+ puts "{"
+ yield
+ puts "}"
+end
+
+surround { puts 'hello world' }
+# {
+# hello world
+# }
``` \ No newline at end of file