From 5424b31848e85e40ea78afb4582e8f33845e1b01 Mon Sep 17 00:00:00 2001 From: Tristan Hume Date: Mon, 12 Aug 2013 14:53:00 -0400 Subject: Explain blocks better --- ruby.html.markdown | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 861a94ad..52321ff6 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -5,6 +5,7 @@ contributors: - ["David Underwood", "http://theflyingdeveloper.com"] - ["Joel Walden", "http://joelwalden.net"] - ["Luke Holder", "http://twitter.com/lukeholder"] + - ["Tristan Hume", "http://thume.ca/"] --- ```ruby @@ -158,11 +159,6 @@ 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} @@ -193,7 +189,12 @@ end # HOWEVER # No-one uses for loops -# Use `each` instead, like this: +# Under the hood for loops use the each method which takes a "block". +# A block is a bunch of code that you can pass to a method like each. +# It is analogous to lambdas, anonymous functions or closures in other programming languages. + +# The each method runs the block multiple times passing a counter. +# You can iterate over a range like this: (1..5).each do |counter| puts "iteration #{counter}" @@ -204,6 +205,17 @@ end #=> iteration 4 #=> iteration 5 +# You can also surround blocks in curly brackets: +(1..5).each {|counter| puts "iteration #{counter}"} + +# You can also iterate over the contents of data structures using each. +array.each do |element| + puts "#{element} is part of the array" +end +hash.each do |key, value| + puts "#{key} is #{value}" +end + counter = 1 while counter <= 5 do puts "iteration #{counter}" -- cgit v1.2.3 From 5c5c8d3c4a0aea7c1c7a8150f428e354d0a12a6c Mon Sep 17 00:00:00 2001 From: Tristan Hume Date: Mon, 12 Aug 2013 15:05:00 -0400 Subject: Tidy up wording --- ruby.html.markdown | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 52321ff6..68c5b524 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -187,14 +187,14 @@ end #=> iteration 4 #=> iteration 5 -# HOWEVER -# No-one uses for loops -# Under the hood for loops use the each method which takes a "block". -# A block is a bunch of code that you can pass to a method like each. +# HOWEVER, No-one uses for loops. +# Instead you should use the "each" method and pass it a block. +# A block is a bunch of code that you can pass to a method like "each". # It is analogous to lambdas, anonymous functions or closures in other programming languages. - -# The each method runs the block multiple times passing a counter. -# You can iterate over a range like this: +# +# The "each" method of a range runs the block once for each element of the range. +# The block is passed a counter as a parameter. +# Calling the "each" method with a block looks like this: (1..5).each do |counter| puts "iteration #{counter}" @@ -208,7 +208,7 @@ end # You can also surround blocks in curly brackets: (1..5).each {|counter| puts "iteration #{counter}"} -# You can also iterate over the contents of data structures using each. +# The contents of data structures can also be iterated using each. array.each do |element| puts "#{element} is part of the array" end -- cgit v1.2.3 From ea853dfa674a1be2c6d5e54b1307bf80693cf01d Mon Sep 17 00:00:00 2001 From: Nick LaMuro Date: Tue, 13 Aug 2013 17:42:03 -0500 Subject: Fixes array typos --- ruby.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 68c5b524..46bfbb7c 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -117,11 +117,11 @@ status == :approved #=> false # Arrays # This is an array -[1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] +array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] # Arrays can contain different types of items -array = [1, "hello", false] #=> => [1, "hello", false] +[1, "hello", false] #=> [1, "hello", false] # Arrays can be indexed # From the front -- cgit v1.2.3 From 7af884a9407c0b79aaede1edd3539e13a55552c8 Mon Sep 17 00:00:00 2001 From: Nick LaMuro Date: Tue, 13 Aug 2013 17:46:33 -0500 Subject: Fixes spacing issues --- ruby.html.markdown | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 46bfbb7c..963d1fc1 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -173,9 +173,9 @@ new_hash.keys #=> [:defcon, :action] if true "if statement" elsif false - "else if, optional" + "else if, optional" else - "else, also optional" + "else, also optional" end for counter in 1..5 @@ -190,7 +190,8 @@ end # HOWEVER, No-one uses for loops. # Instead you should use the "each" method and pass it a block. # A block is a bunch of code that you can pass to a method like "each". -# It is analogous to lambdas, anonymous functions or closures in other programming languages. +# It is analogous to lambdas, anonymous functions or closures in other +# programming languages. # # The "each" method of a range runs the block once for each element of the range. # The block is passed a counter as a parameter. -- cgit v1.2.3 From 9fd70ffbb12e92a4bd129cfbe28d91e07138c398 Mon Sep 17 00:00:00 2001 From: Nick LaMuro Date: Tue, 13 Aug 2013 17:47:52 -0500 Subject: Giving myself some credit... I guess --- ruby.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 963d1fc1..19f2ec86 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -6,6 +6,7 @@ contributors: - ["Joel Walden", "http://joelwalden.net"] - ["Luke Holder", "http://twitter.com/lukeholder"] - ["Tristan Hume", "http://thume.ca/"] + - ["Nick LaMuro", "https://github.com/NickLaMuro"] --- ```ruby -- cgit v1.2.3 From 6062618d358839e22f4bbe6348658253ad2e0209 Mon Sep 17 00:00:00 2001 From: Geoffrey Roguelon Date: Wed, 14 Aug 2013 19:11:13 +0200 Subject: Fix a missing character in the comment. remove useless whitespace at the end of the lines. --- ruby.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 19f2ec86..3a233d98 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -36,7 +36,7 @@ You shouldn't either # Arithmetic is just syntactic sugar # for calling a method on an object 1.+(3) #=> 4 -10.* 5 #=> 50 +10.* 5 #=> 50 # Special values are objects nil # Nothing to see here @@ -242,7 +242,7 @@ when 'D' puts "Scraping through" when 'F' puts "You failed!" -else +else puts "Alternative grading system, eh?" end @@ -252,7 +252,7 @@ def double(x) x * 2 end -# Functions (and all blocks) implcitly return the value of the last statement +# Functions (and all blocks) implicitly return the value of the last statement double(2) #=> 4 # Parentheses are optional where the result is unambiguous -- cgit v1.2.3