From 8de5c352bc94bf6f1e5886e68356b53e0ffd80d4 Mon Sep 17 00:00:00 2001 From: David Underwood Date: Fri, 28 Jun 2013 15:57:57 -0400 Subject: Initial WIP commit for ruby --- ruby.html.markdown | 202 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 202 insertions(+) create mode 100644 ruby.html.markdown (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown new file mode 100644 index 00000000..32dfeff3 --- /dev/null +++ b/ruby.html.markdown @@ -0,0 +1,202 @@ +--- +language: ruby +author: David Underwood +author_url: http://theflyingdeveloper.com +--- + +```ruby +# This is a comment + +=begin +This is a multiline comment +No-one uses them +You shouldn't either +=end + + +3 #=> 3 + + +# Some basic arithmetic +1 + 1 #=> 2 +8 - 1 #=> 7 +10 * 2 #=> 20 +35 / 5 #=> 7 + +# Special values +nil #=> Nothing to see here +true #=> truth +false #=> falsehood + +# Equality +1 == 1 #=> true +2 == 1 #=> false + +# Inequality +1 != 1 #=> false +2 != 1 #=> true +!true #=> false +!false #=> true + +# More comparisons +1 < 10 #=> True +1 > 10 #=> False +2 <= 2 #=> True +2 >= 2 #=> True + +'I am a string' +"I am a string too" + +placeholder = "use variables inline" +"I can #{placeholder} when using double quoted strings" +#=> "I can use variables inline when using double quoted strings" + + +# print to the output +puts "I'm printing!" + +# Variables +x = 25 #=> 25 + +# Note that assignment returns the value assigned +# This means you can do multiple assignment: + +x = y = 10 #=> 10 +x #=> 10 +y #=> 10 + +# By convention, use snake_case for variable names +snake_case = true + +# Use descriptive variable names +path_to_project_root = '/good/name/' +path = '/bad/name/' + +# Arrays + +# This is an 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] + +# Arrays can be indexed +# From the front +array[0] #=> 1 +array[12] #=> nil + +# From the end +array[-1] #=> 5 + +# With a start and end index +array[2, 4] #=> [3, 4, 5] + +# Or with a range +array[1..3] #=> [2, 3, 4] + +# Add to the end of an array like this +array << 6 #=> [1, 2, 3, 4, 5, 6] + +# Or like this +array.push 7 #=> [1, 2, 3, 4, 5, 6, 7] + +# Or to the beginning like this +array.unshift 0 #=> [0, 1, 2, 3, 4, 5, 6, 7] + +# Remove the first item in an array + +array.shift #=> [1, 2, 3, 4, 5, 6, 7] + +# Or the last + +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. + +# Control structures + +if true + "if statement" +elsif false + "else if, optional" +else + "else, also optional" +end + +for counter in 1..5 + puts "iteration #{counter}" +end +#=> iteration 1 +#=> iteration 2 +#=> iteration 3 +#=> iteration 4 +#=> iteration 5 + +# HOWEVER +# No-one uses for loops +# Use `each` instead, like this: + +(1..5).each do |counter| + puts "iteration #{counter}" +end +#=> iteration 1 +#=> iteration 2 +#=> iteration 3 +#=> iteration 4 +#=> iteration 5 + +counter = 1 +while counter <= 5 do + puts "iteration #{counter}" +end +#=> iteration 1 +#=> iteration 2 +#=> iteration 3 +#=> iteration 4 +#=> iteration 5 + +grade = 'B' +case grade +when 'A' + puts "way to go kiddo" +when 'B' + puts "Better luck next time" +when 'C' + puts "You can do better" +when 'D' + puts "Scraping through" +when 'F' + puts "You failed!" + + +# Functions + +def double(x) + x * 2 +end + +# Functions (and all blocks) implcitly return the value of the last statement +double(2) #=> 4 + +# Parentheses are optional where the result is unambiguous +double 3 #=> 6 + +double double 3 #=> 12 + +def sum(x,y) + x + y +end + +# Method arguments are separated by a comma +sum 3, 4 #=> 7 + +sum sum(3,4), 5 #=> 12 + +# yield +# All methods have an implicit, optional block parameter +# it can be called with the 'yield' keyword + + +``` \ No newline at end of file -- cgit v1.2.3 From b9e0c189ee856de31a3fd035b39bd54765104a8d Mon Sep 17 00:00:00 2001 From: David Underwood Date: Sat, 29 Jun 2013 18:04:37 -0400 Subject: fixes typos in the boolean results --- ruby.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 32dfeff3..a317c498 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -39,10 +39,10 @@ false #=> falsehood !false #=> true # More comparisons -1 < 10 #=> True -1 > 10 #=> False -2 <= 2 #=> True -2 >= 2 #=> True +1 < 10 #=> true +1 > 10 #=> false +2 <= 2 #=> true +2 >= 2 #=> true 'I am a string' "I am a string too" -- cgit v1.2.3 From 07aa1d9337252eea9f4d5103e028c894132be91f Mon Sep 17 00:00:00 2001 From: David Underwood Date: Mon, 1 Jul 2013 12:27:15 -0400 Subject: Adds sections for symbols, hashes, and yields --- ruby.html.markdown | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) (limited to 'ruby.html.markdown') 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 -- cgit v1.2.3 From 918123e8cef7047550bf3bb9dbecc9b3efb01de2 Mon Sep 17 00:00:00 2001 From: David Underwood Date: Tue, 2 Jul 2013 13:24:50 -0400 Subject: changes string interpolation text as suggested by @fbernier --- 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 b07c92c9..44b0e46c 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -47,9 +47,9 @@ false #=> falsehood 'I am a string' "I am a string too" -placeholder = "use variables inline" +placeholder = "use string interpolation" "I can #{placeholder} when using double quoted strings" -#=> "I can use variables inline when using double quoted strings" +#=> "I can use string interpolation when using double quoted strings" # print to the output -- cgit v1.2.3 From 705e8fd023f6c61e4ccb79095238474ee922a0bd Mon Sep 17 00:00:00 2001 From: David Underwood Date: Tue, 2 Jul 2013 13:46:58 -0400 Subject: Adds emphasis about the object-oriented nature of ruby, and removes references to lesser-used array functions --- ruby.html.markdown | 59 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 28 deletions(-) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 44b0e46c..62db549c 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -13,8 +13,13 @@ No-one uses them You shouldn't either =end +# First and foremost: Everything is an object. -3 #=> 3 +# Numbers are objects + +3.class #=> Fixnum + +3.to_s #=> "3" # Some basic arithmetic @@ -23,15 +28,24 @@ You shouldn't either 10 * 2 #=> 20 35 / 5 #=> 7 -# Special values -nil #=> Nothing to see here -true #=> truth -false #=> falsehood +# Special values are objects +nil # Nothing to see here +true # truth +false # falsehood + +nil.class #=> NilClass +true.class #=> TrueClass +false.class #=> FalseClass # Equality 1 == 1 #=> true 2 == 1 #=> false +# apart from false itself, nil is the only other 'falsey' value + +nil == false #=> true +0 == false #=> false + # Inequality 1 != 1 #=> false 2 != 1 #=> true @@ -44,8 +58,10 @@ false #=> falsehood 2 <= 2 #=> true 2 >= 2 #=> true -'I am a string' -"I am a string too" +# Strings are objects + +'I am a string'.class #=> String +"I am a string too".class #=> String placeholder = "use string interpolation" "I can #{placeholder} when using double quoted strings" @@ -57,6 +73,7 @@ puts "I'm printing!" # Variables x = 25 #=> 25 +x #=> 25 # Note that assignment returns the value assigned # This means you can do multiple assignment: @@ -72,17 +89,19 @@ snake_case = true path_to_project_root = '/good/name/' path = '/bad/name/' -# Symbols +# Symbols (are objects) # Symbols are immutable, reusable constants represented internally by an integer value # They're often used instead of strings to efficiently convey specific, meaningful values +:pending.class #=> Symbol + status = :pending status == :pending #=> true status == 'pending' #=> false -position = :left +status == :approved #=> false # Arrays @@ -107,26 +126,9 @@ array[2, 4] #=> [3, 4, 5] # Or with a range array[1..3] #=> [2, 3, 4] -# Add to the end of an array like this +# Add to an array like this array << 6 #=> [1, 2, 3, 4, 5, 6] -# Or like this -array.push 7 #=> [1, 2, 3, 4, 5, 6, 7] - -# Or to the beginning like this -array.unshift 0 #=> [0, 1, 2, 3, 4, 5, 6, 7] - -# Remove the first item in an array - -array.shift #=> [1, 2, 3, 4, 5, 6, 7] - -# Or the last - -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} @@ -152,7 +154,7 @@ 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 +# This means they share a lot of useful methods such as each, map, count, and more # Control structures @@ -197,6 +199,7 @@ end #=> iteration 5 grade = 'B' + case grade when 'A' puts "Way to go kiddo" -- cgit v1.2.3 From 99192fea522aa80ec01aeb7d4e570ee24088981b Mon Sep 17 00:00:00 2001 From: David Underwood Date: Wed, 3 Jul 2013 15:21:29 -0400 Subject: Adds class documentation written by @joelwalden in #95 --- ruby.html.markdown | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'ruby.html.markdown') diff --git a/ruby.html.markdown b/ruby.html.markdown index 62db549c..7863ebd2 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -251,4 +251,56 @@ surround { puts 'hello world' } # { # hello world # } + + +# Define a class with the class keyword +class Human + + # A class variable. It is shared by all instances of this class. + @@species = "H. sapiens" + + # Basic initializer + def initialize(name, age=0): + # Assign the argument to the "name" instance variable for the instance + @name = name + # If no age given, we will fall back to the default in the arguments list. + @age = age + end + + # Basic setter method + def name=(name) + @name = name + end + + # Basic getter method + def name + @name + end + + # A class method; uses self to distinguish from instance methods. (Can only be called on class, not an instance). + + def self.say(msg) + puts "#{msg}" + end + + def species + @@species + end + +end + + +# Instantiate a class +jim = Human.new("Jim Halpert") + +dwight = Human.new("Dwight K. Schrute") + +# Let's call a couple of methods +jim.species #=> "H. sapiens" +jim.name #=> "Jim Halpert" +dwight.species #=> "H. sapiens" +dwight.name #=> "Dwight K. Schrute" + +# Call the class method +Human.say("Hi") #=> "Hi" ``` \ No newline at end of file -- cgit v1.2.3 From 2419d8c30aada12c72f0c7ee36db58298263e110 Mon Sep 17 00:00:00 2001 From: David Underwood Date: Wed, 3 Jul 2013 15:39:43 -0400 Subject: Updates YAML front-matter to match #97 --- 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 7863ebd2..2de134ae 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -1,7 +1,7 @@ --- language: ruby -author: David Underwood -author_url: http://theflyingdeveloper.com +contributors: + - ["David Underwood", "http://theflyingdeveloper.com"] --- ```ruby -- cgit v1.2.3