From 20d5b4057b44ffcf92478b2a8e9476ace2fdc0f5 Mon Sep 17 00:00:00 2001 From: Amir Rajan Date: Tue, 22 Sep 2020 06:27:46 -0500 Subject: synced with 1.22 --- .../app/01_printing.txt | 31 +++ .../app/02_printing_to_the_console.txt | 31 --- .../00_intermediate_ruby_primer/app/02_strings.txt | 15 ++ .../00_intermediate_ruby_primer/app/03_numbers.txt | 21 +++ .../00_intermediate_ruby_primer/app/03_strings.txt | 15 -- .../app/04_booleans.txt | 32 ++++ .../00_intermediate_ruby_primer/app/04_numbers.txt | 21 --- .../app/05_booleans.txt | 32 ---- .../app/05_conditionals.txt | 114 +++++++++++ .../app/06_conditionals.txt | 114 ----------- .../00_intermediate_ruby_primer/app/06_looping.txt | 55 ++++++ .../app/07_functions.txt | 69 +++++++ .../00_intermediate_ruby_primer/app/07_looping.txt | 55 ------ .../00_intermediate_ruby_primer/app/08_arrays.txt | 210 +++++++++++++++++++++ .../app/08_functions.txt | 69 ------- .../app/09_powerful_arrays.txt | 210 --------------------- 16 files changed, 547 insertions(+), 547 deletions(-) create mode 100644 samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/01_printing.txt delete mode 100644 samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/02_printing_to_the_console.txt create mode 100644 samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/02_strings.txt create mode 100644 samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/03_numbers.txt delete mode 100644 samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/03_strings.txt create mode 100644 samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/04_booleans.txt delete mode 100644 samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/04_numbers.txt delete mode 100644 samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/05_booleans.txt create mode 100644 samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/05_conditionals.txt delete mode 100644 samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/06_conditionals.txt create mode 100644 samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/06_looping.txt create mode 100644 samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/07_functions.txt delete mode 100644 samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/07_looping.txt create mode 100644 samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/08_arrays.txt delete mode 100644 samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/08_functions.txt delete mode 100644 samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/09_powerful_arrays.txt (limited to 'samples/00_learn_ruby_optional') diff --git a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/01_printing.txt b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/01_printing.txt new file mode 100644 index 0000000..dd86367 --- /dev/null +++ b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/01_printing.txt @@ -0,0 +1,31 @@ +# ==================================================================================== +# Commenting Code +# ==================================================================================== +# +# Prefixing text with a pound sign (#) is how you comment code in Ruby. Example: +# +# I am commented code. And so are the lines above. +# +# I you want more than a quick primer on Ruby, check out https://poignant.guide/. It's +# an entertaining read. Otherwise, go to the next txt file. +# +# Follow along by visiting: +# https://s3.amazonaws.com/s3.dragonruby.org/dragonruby-gtk-intermediate.mp4 + +# ==================================================================================== +# Printing to the Console: +# ==================================================================================== +# +# Every time you save repl.rb file, DragonRuby runs the code within it. Copy this text +# to repl.rb and save to see Hello World printed to the console. + +repl do + puts '* RUBY PRIMER: Printing to the console using the ~puts~ function.' + puts '====' + puts '======' + puts '================================' + puts 'Hello World' + puts '================================' + puts '======' + puts '====' +end diff --git a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/02_printing_to_the_console.txt b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/02_printing_to_the_console.txt deleted file mode 100644 index dd86367..0000000 --- a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/02_printing_to_the_console.txt +++ /dev/null @@ -1,31 +0,0 @@ -# ==================================================================================== -# Commenting Code -# ==================================================================================== -# -# Prefixing text with a pound sign (#) is how you comment code in Ruby. Example: -# -# I am commented code. And so are the lines above. -# -# I you want more than a quick primer on Ruby, check out https://poignant.guide/. It's -# an entertaining read. Otherwise, go to the next txt file. -# -# Follow along by visiting: -# https://s3.amazonaws.com/s3.dragonruby.org/dragonruby-gtk-intermediate.mp4 - -# ==================================================================================== -# Printing to the Console: -# ==================================================================================== -# -# Every time you save repl.rb file, DragonRuby runs the code within it. Copy this text -# to repl.rb and save to see Hello World printed to the console. - -repl do - puts '* RUBY PRIMER: Printing to the console using the ~puts~ function.' - puts '====' - puts '======' - puts '================================' - puts 'Hello World' - puts '================================' - puts '======' - puts '====' -end diff --git a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/02_strings.txt b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/02_strings.txt new file mode 100644 index 0000000..34ea252 --- /dev/null +++ b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/02_strings.txt @@ -0,0 +1,15 @@ +# ==================================================================================== +# Strings +# ==================================================================================== +# +# Here is how you work with strings in Ruby. Take the text +# in this file and paste it into repl.rb and save: + +repl do + puts '* RUBY PRIMER: strings' + message = "Hello World" + puts "The value of message is: " + message + puts "Any value can be interpolated within a string using \#{}." + puts "Interpolated message: #{message}." + puts 'This #{message} is not interpolated because the string uses single quotes.' +end diff --git a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/03_numbers.txt b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/03_numbers.txt new file mode 100644 index 0000000..dfdf04d --- /dev/null +++ b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/03_numbers.txt @@ -0,0 +1,21 @@ +# ==================================================================================== +# Numerics +# ==================================================================================== +# +# Here is how you work with numbers in Ruby. Take the text +# in this file and paste it into repl.rb and save: + +repl do + puts '* RUBY PRIMER: Fixnum and Floats' + a = 10 + puts "The value of a is: #{a}" + puts "a + 1 is: #{a + 1}" + puts "a / 3 is: #{a / 3}" + puts '' + + b = 10.12 + puts "The value of b is: #{b}" + puts "b + 1 is: #{b + 1}" + puts "b as an integer is: #{b.to_i}" + puts '' +end diff --git a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/03_strings.txt b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/03_strings.txt deleted file mode 100644 index 34ea252..0000000 --- a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/03_strings.txt +++ /dev/null @@ -1,15 +0,0 @@ -# ==================================================================================== -# Strings -# ==================================================================================== -# -# Here is how you work with strings in Ruby. Take the text -# in this file and paste it into repl.rb and save: - -repl do - puts '* RUBY PRIMER: strings' - message = "Hello World" - puts "The value of message is: " + message - puts "Any value can be interpolated within a string using \#{}." - puts "Interpolated message: #{message}." - puts 'This #{message} is not interpolated because the string uses single quotes.' -end diff --git a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/04_booleans.txt b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/04_booleans.txt new file mode 100644 index 0000000..2a9060f --- /dev/null +++ b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/04_booleans.txt @@ -0,0 +1,32 @@ +# ==================================================================================== +# Booleans +# ==================================================================================== +# +# Here is how you work with numbers in Ruby. Take the text +# in this file and paste it into repl.rb and save: + +repl do + puts '* RUBY PRIMER: TrueClass, FalseClass, NilClass (truthy / falsey values)' + puts "Anything that *isn't* false or nil is true." + + c = 30 + puts "The value of c is #{c}." + + if c + puts "This if statement ran because c is truthy." + end + + d = false + puts "The value if d is #{d}. The type for d is #{d.class}." + + if !d + puts "This if statement ran because d is falsey, using the not operator (!)." + end + + e = nil + puts "Nil is also considered falsey. The value of e is: #{e} (a blank string when printed). Which is of type #{e.class}." + + if !e + puts "This if statement ran because e is nil and the if statement applied the NOT operator. !e yields a type of #{(!e).class}." + end +end diff --git a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/04_numbers.txt b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/04_numbers.txt deleted file mode 100644 index dfdf04d..0000000 --- a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/04_numbers.txt +++ /dev/null @@ -1,21 +0,0 @@ -# ==================================================================================== -# Numerics -# ==================================================================================== -# -# Here is how you work with numbers in Ruby. Take the text -# in this file and paste it into repl.rb and save: - -repl do - puts '* RUBY PRIMER: Fixnum and Floats' - a = 10 - puts "The value of a is: #{a}" - puts "a + 1 is: #{a + 1}" - puts "a / 3 is: #{a / 3}" - puts '' - - b = 10.12 - puts "The value of b is: #{b}" - puts "b + 1 is: #{b + 1}" - puts "b as an integer is: #{b.to_i}" - puts '' -end diff --git a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/05_booleans.txt b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/05_booleans.txt deleted file mode 100644 index 2a9060f..0000000 --- a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/05_booleans.txt +++ /dev/null @@ -1,32 +0,0 @@ -# ==================================================================================== -# Booleans -# ==================================================================================== -# -# Here is how you work with numbers in Ruby. Take the text -# in this file and paste it into repl.rb and save: - -repl do - puts '* RUBY PRIMER: TrueClass, FalseClass, NilClass (truthy / falsey values)' - puts "Anything that *isn't* false or nil is true." - - c = 30 - puts "The value of c is #{c}." - - if c - puts "This if statement ran because c is truthy." - end - - d = false - puts "The value if d is #{d}. The type for d is #{d.class}." - - if !d - puts "This if statement ran because d is falsey, using the not operator (!)." - end - - e = nil - puts "Nil is also considered falsey. The value of e is: #{e} (a blank string when printed). Which is of type #{e.class}." - - if !e - puts "This if statement ran because e is nil and the if statement applied the NOT operator. !e yields a type of #{(!e).class}." - end -end diff --git a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/05_conditionals.txt b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/05_conditionals.txt new file mode 100644 index 0000000..8a0c172 --- /dev/null +++ b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/05_conditionals.txt @@ -0,0 +1,114 @@ +# ==================================================================================== +# Conditionals +# ==================================================================================== +# +# Here is how you create conditionals in Ruby. Take the text +# in this file and paste it into repl.rb and save: + +repl do + puts "* RUBY PRIMER: Conditionals" +end + +# ==================================================================================== +# if +# ==================================================================================== + +repl do + puts "** INFO: if statement" + i_am_one = 1 + if i_am_one + puts "This was printed because i_am_one is truthy." + end +end + +# ==================================================================================== +# if/else +# ==================================================================================== + +repl do + puts "** INFO: if/else statement" + i_am_false = false + if i_am_false + puts "This will NOT get printed because i_am_false is false." + else + puts "This was printed because i_am_false is false." + end +end + + +# ==================================================================================== +# if/elsif/else +# ==================================================================================== + +repl do + puts "** INFO: if/elsif/else statement" + i_am_false = false + i_am_true = true + if i_am_false + puts "This will NOT get printed because i_am_false is false." + elsif i_am_true + puts "This was printed because i_am_true is true." + else + puts "This will NOT get printed i_am_true was true." + end +end + +# ==================================================================================== +# case +# ==================================================================================== + +repl do + puts "** INFO case statement" + i_am_one = 1 # change this value to see different results + + case i_am_one + when 10 + puts "the value of i_am_one is 10" + when 9 + puts "the value of i_am_one is 9" + when 5 + puts "the value of i_am_one is 5" + when 1 + puts "the value of i_am_one is 1" + else + puts "Value wasn't cased." + end +end + +# ==================================================================================== +# comparison operators +# ==================================================================================== + +repl do + puts "** INFO: Different types of comparisons" + if 4 == 4 + puts "4 equals 4 (==)" + end + + if 4 != 3 + puts "4 does not equal 3 (!=)" + end + + if 3 < 4 + puts "3 is less than 4 (<)" + end + + if 4 > 3 + puts "4 is greater than 3 (>)" + end +end + +# ==================================================================================== +# and/or conditionals +# ==================================================================================== + +repl do + puts "** INFO: AND, OR operator (&&, ||)" + if (4 > 3) || (3 < 4) || false + puts "print this if 4 is greater than 3 OR 3 is less than 4 OR false is true (||)" + end + + if (4 > 3) && (3 < 4) + puts "print this if 4 is greater than 3 AND 3 is less than 4 (&&)" + end +end diff --git a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/06_conditionals.txt b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/06_conditionals.txt deleted file mode 100644 index 8a0c172..0000000 --- a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/06_conditionals.txt +++ /dev/null @@ -1,114 +0,0 @@ -# ==================================================================================== -# Conditionals -# ==================================================================================== -# -# Here is how you create conditionals in Ruby. Take the text -# in this file and paste it into repl.rb and save: - -repl do - puts "* RUBY PRIMER: Conditionals" -end - -# ==================================================================================== -# if -# ==================================================================================== - -repl do - puts "** INFO: if statement" - i_am_one = 1 - if i_am_one - puts "This was printed because i_am_one is truthy." - end -end - -# ==================================================================================== -# if/else -# ==================================================================================== - -repl do - puts "** INFO: if/else statement" - i_am_false = false - if i_am_false - puts "This will NOT get printed because i_am_false is false." - else - puts "This was printed because i_am_false is false." - end -end - - -# ==================================================================================== -# if/elsif/else -# ==================================================================================== - -repl do - puts "** INFO: if/elsif/else statement" - i_am_false = false - i_am_true = true - if i_am_false - puts "This will NOT get printed because i_am_false is false." - elsif i_am_true - puts "This was printed because i_am_true is true." - else - puts "This will NOT get printed i_am_true was true." - end -end - -# ==================================================================================== -# case -# ==================================================================================== - -repl do - puts "** INFO case statement" - i_am_one = 1 # change this value to see different results - - case i_am_one - when 10 - puts "the value of i_am_one is 10" - when 9 - puts "the value of i_am_one is 9" - when 5 - puts "the value of i_am_one is 5" - when 1 - puts "the value of i_am_one is 1" - else - puts "Value wasn't cased." - end -end - -# ==================================================================================== -# comparison operators -# ==================================================================================== - -repl do - puts "** INFO: Different types of comparisons" - if 4 == 4 - puts "4 equals 4 (==)" - end - - if 4 != 3 - puts "4 does not equal 3 (!=)" - end - - if 3 < 4 - puts "3 is less than 4 (<)" - end - - if 4 > 3 - puts "4 is greater than 3 (>)" - end -end - -# ==================================================================================== -# and/or conditionals -# ==================================================================================== - -repl do - puts "** INFO: AND, OR operator (&&, ||)" - if (4 > 3) || (3 < 4) || false - puts "print this if 4 is greater than 3 OR 3 is less than 4 OR false is true (||)" - end - - if (4 > 3) && (3 < 4) - puts "print this if 4 is greater than 3 AND 3 is less than 4 (&&)" - end -end diff --git a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/06_looping.txt b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/06_looping.txt new file mode 100644 index 0000000..03c3d28 --- /dev/null +++ b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/06_looping.txt @@ -0,0 +1,55 @@ +# ==================================================================================== +# Looping +# ==================================================================================== +# +# Looping looks a whole lot different than other languages. +# But it's pretty awesome when you get used to it. + +repl do + puts "* RUBY PRIMER: Loops" +end + +# ==================================================================================== +# times +# ==================================================================================== + +repl do + puts "** INFO: ~Numeric#times~ (for loop)" + 3.times do |i| + puts i + end +end + +# ==================================================================================== +# foreach +# ==================================================================================== + +repl do + puts "** INFO: ~Array#each~ (for each loop)" + array = ["a", "b", "c", "d"] + array.each do |char| + puts char + end + + puts "** INFO: ~Array#each_with_index~ (for each loop)" + array = ["a", "b", "c", "d"] + array.each do |char, i| + puts "index #{i}: #{char}" + end +end + +# ==================================================================================== +# ranges +# ==================================================================================== + +repl do + puts "** INFO: range block exclusive (three dots)" + (0...3).each do |i| + puts i + end + + puts "** INFO: range block inclusive (two dots)" + (0..3).each do |i| + puts i + end +end diff --git a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/07_functions.txt b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/07_functions.txt new file mode 100644 index 0000000..9ad38de --- /dev/null +++ b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/07_functions.txt @@ -0,0 +1,69 @@ +# ==================================================================================== +# Functions +# ==================================================================================== + +# The last statement of a function is implictly returned. Parenthesis for functions +# are optional as long as the statement can be envaluated disambiguously. + +repl do + puts "* RUBY PRIMER: Functions" +end + +# ==================================================================================== +# Functions single parameter +# ==================================================================================== + +repl do + puts "* INFO: Function with one parameter" + + # function definition + def add_one_to n + n + 1 + end + + # Parenthesis are optional in Ruby as long as the + # parsing is disambiguous. Here are a couple of variations. + # Generally speaking, don't put parenthesis is you don't have to. + + # Conventional Usage of Parenthesis. + puts add_one_to(3) + + # DragonRuby's recommended use of parenthesis (inner function has parenthesis). + puts (add_one_to 3) + + # Full parens. + puts(add_one_to(3)) + + # Outer function has parenthesis + puts(add_one_to 3) +end + +# ==================================================================================== +# Functions with default parameter values +# ==================================================================================== + +repl do + puts "* INFO: Function with default value" + def function_with_default_value v = 10 + v * 10 + end + + puts "Passing the argument three yields: #{function_with_default_value 3}" + puts "Passing no argument yields: #{function_with_default_value}" +end + +# ==================================================================================== +# Nil default parameter value and ||= operator. +# ==================================================================================== + +repl do + puts "* INFO: Using the OR EQUAL operator (||=)" + def function_with_nil_default_with_local a = nil + result = a + result ||= "DEFAULT_VALUE_OF_A_IS_NIL_OR_FALSE" + "value is #{result}." + end + + puts "Passing 'hi' as the argument yields: #{function_with_nil_default_with_local 'hi'}" + puts "Passing nil: #{function_with_nil_default_with_local}" +end diff --git a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/07_looping.txt b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/07_looping.txt deleted file mode 100644 index 03c3d28..0000000 --- a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/07_looping.txt +++ /dev/null @@ -1,55 +0,0 @@ -# ==================================================================================== -# Looping -# ==================================================================================== -# -# Looping looks a whole lot different than other languages. -# But it's pretty awesome when you get used to it. - -repl do - puts "* RUBY PRIMER: Loops" -end - -# ==================================================================================== -# times -# ==================================================================================== - -repl do - puts "** INFO: ~Numeric#times~ (for loop)" - 3.times do |i| - puts i - end -end - -# ==================================================================================== -# foreach -# ==================================================================================== - -repl do - puts "** INFO: ~Array#each~ (for each loop)" - array = ["a", "b", "c", "d"] - array.each do |char| - puts char - end - - puts "** INFO: ~Array#each_with_index~ (for each loop)" - array = ["a", "b", "c", "d"] - array.each do |char, i| - puts "index #{i}: #{char}" - end -end - -# ==================================================================================== -# ranges -# ==================================================================================== - -repl do - puts "** INFO: range block exclusive (three dots)" - (0...3).each do |i| - puts i - end - - puts "** INFO: range block inclusive (two dots)" - (0..3).each do |i| - puts i - end -end diff --git a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/08_arrays.txt b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/08_arrays.txt new file mode 100644 index 0000000..9904686 --- /dev/null +++ b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/08_arrays.txt @@ -0,0 +1,210 @@ +# ==================================================================================== +# Arrays +# ==================================================================================== + +# Arrays are incredibly powerful in Ruby. Learn to use them well. + +repl do + puts "* RUBY PRIMER: ARRAYS" +end + +# ==================================================================================== +# Enumerable ranges and .to_a +# ==================================================================================== + +repl do + puts "** INFO: Create an array with the numbers 1 to 10." + one_to_ten = (1..10).to_a + puts one_to_ten +end + +# ==================================================================================== +# Finding elements +# ==================================================================================== + +repl do + puts "** INFO: Finding elements in an array using ~Array#find_all~." + puts "Create a new array that only contains even numbers from the previous array." + + one_to_ten = (1..10).to_a + evens = one_to_ten.find_all do |number| + number % 2 == 0 + end + + puts evens +end + +# ==================================================================================== +# Rejecting elements +# ==================================================================================== + +repl do + puts "** INFO: Removing elements in an array using ~Array#reject~." + puts "Create a new array that rejects odd numbers." + + one_to_ten = (1..10).to_a + also_even = one_to_ten.reject do |number| + number % 2 != 0 + end + + puts also_even +end + +# ==================================================================================== +# Array transform using the map function. +# ==================================================================================== + +repl do + puts "** INFO: Creating new derived values from an array using ~Array#map~." + puts "Create an array that doubles every number." + + one_to_ten = (1..10).to_a + doubled = one_to_ten.map do |number| + number * 2 + end + + puts doubled +end + +# ==================================================================================== +# Combining array functions. +# ==================================================================================== + +repl do + puts "** INFO: Combining ~Array#find_all~ along with ~Array#map~." + puts "Create an array that selects only odd numbers and then multiply those by 10." + + one_to_ten = (1..10).to_a + odd_doubled = one_to_ten.find_all do |number| + number % 2 != 0 + end.map do |odd_number| + odd_number * 10 + end + + puts odd_doubled +end + +# ==================================================================================== +# Product function. +# ==================================================================================== + +repl do + puts "** INFO: Create all combinations of array values using ~Array#product~." + puts "All two-item pairs of numbers 1 to 10." + one_to_ten = (1..10).to_a + all_combinations = one_to_ten.product(one_to_ten) + puts all_combinations +end + +# ==================================================================================== +# Uniq and sort function. +# ==================================================================================== + +repl do + puts "** INFO: Providing uniq values using ~Array#uniq~ and ~Array#sort~." + puts "All uniq combinations of numbers regardless of order." + puts "For example: [1, 2] is the same as [2, 1]." + one_to_ten = (1..10).to_a + uniq_combinations = + one_to_ten.product(one_to_ten) + .map do |unsorted_number| + unsorted_number.sort + end.uniq + puts uniq_combinations +end + +# ==================================================================================== +# Example of an advanced array transform. +# ==================================================================================== + +repl do + puts "** INFO: Advanced chaining. Combining ~Array's ~map~, ~find_all~, ~sort~, and ~sort_by~." + puts "All unique Pythagorean Triples between 1 and 100 sorted by area of the triangle." + + one_to_hundred = (1..100).to_a + + triples = + one_to_hundred.product(one_to_hundred).map do |width, height| + [width, height, Math.sqrt(width ** 2 + height ** 2)] + end.find_all do |_, _, hypotenuse| + hypotenuse.to_i == hypotenuse + end.map do |triangle| + triangle.map(&:to_i) + end.uniq do |triangle| + triangle.sort + end.map do |width, height, hypotenuse| + [width, height, hypotenuse, (width * height) / 2] + end.sort_by do |_, _, _, area| + area + end + + triples.each do |width, height, hypotenuse, _| + puts "(#{width}, #{height}, #{hypotenuse})" + end +end + +# ==================================================================================== +# Example of an sorting. +# ==================================================================================== + +repl do + puts "** INFO: Implementing a custom sort function that operates on the ~Hash~ datatype." + + things_to_sort = [ + { type: :background, order: 1 }, + { type: :foreground, order: 1 }, + { type: :foreground, order: 2 } + ] + puts "*** Original array." + puts things_to_sort + + puts "*** Simple sort using key." + # For a simple sort, you can use sort_by + results = things_to_sort.sort_by do |hash| + hash[:order] + end + + puts results + + puts "*** Custom sort." + puts "**** Sorting process." + # for a more complicated sort, you can provide a block that returns + # -1, 0, 1 for a left and right operand + results = things_to_sort.sort do |l, r| + sort_result = 0 + puts "here is l: #{l}" + puts "here is r: #{r || "nil"}" + # if either value is nil/false return 0 + if !l || !r + sort_result = 0 + # if the type of "left" is background and the + # type of "right" is foreground, then return + # -1 (which means "left" is less than "right" + elsif l[:type] == :background && r[:type] == :foreground + sort_result = -1 + # if the type of "left" is foreground and the + # type of "right" is background, then return + # 1 (which means "left" is greater than "right" + elsif l[:type] == :foreground && r[:type] == :background + sort_result = 1 + # if "left" and "right"'s type are the same, then + # use the order as the tie breaker + elsif l[:order] < r[:order] + sort_result = -1 + elsif l[:order] > r[:order] + sort_result = 1 + # returning 0 means both values are equal + else + sort_result = 0 + end + sort_result + end.to_a + + puts "**** Sort result." + puts results +end + +# ==================================================================================== +# Api documention for Array that is worth commiting to memory because arrays are so +# awesome in Ruby: https://docs.ruby-lang.org/en/2.0.0/Array.html +# ==================================================================================== diff --git a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/08_functions.txt b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/08_functions.txt deleted file mode 100644 index 9ad38de..0000000 --- a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/08_functions.txt +++ /dev/null @@ -1,69 +0,0 @@ -# ==================================================================================== -# Functions -# ==================================================================================== - -# The last statement of a function is implictly returned. Parenthesis for functions -# are optional as long as the statement can be envaluated disambiguously. - -repl do - puts "* RUBY PRIMER: Functions" -end - -# ==================================================================================== -# Functions single parameter -# ==================================================================================== - -repl do - puts "* INFO: Function with one parameter" - - # function definition - def add_one_to n - n + 1 - end - - # Parenthesis are optional in Ruby as long as the - # parsing is disambiguous. Here are a couple of variations. - # Generally speaking, don't put parenthesis is you don't have to. - - # Conventional Usage of Parenthesis. - puts add_one_to(3) - - # DragonRuby's recommended use of parenthesis (inner function has parenthesis). - puts (add_one_to 3) - - # Full parens. - puts(add_one_to(3)) - - # Outer function has parenthesis - puts(add_one_to 3) -end - -# ==================================================================================== -# Functions with default parameter values -# ==================================================================================== - -repl do - puts "* INFO: Function with default value" - def function_with_default_value v = 10 - v * 10 - end - - puts "Passing the argument three yields: #{function_with_default_value 3}" - puts "Passing no argument yields: #{function_with_default_value}" -end - -# ==================================================================================== -# Nil default parameter value and ||= operator. -# ==================================================================================== - -repl do - puts "* INFO: Using the OR EQUAL operator (||=)" - def function_with_nil_default_with_local a = nil - result = a - result ||= "DEFAULT_VALUE_OF_A_IS_NIL_OR_FALSE" - "value is #{result}." - end - - puts "Passing 'hi' as the argument yields: #{function_with_nil_default_with_local 'hi'}" - puts "Passing nil: #{function_with_nil_default_with_local}" -end diff --git a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/09_powerful_arrays.txt b/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/09_powerful_arrays.txt deleted file mode 100644 index 9904686..0000000 --- a/samples/00_learn_ruby_optional/00_intermediate_ruby_primer/app/09_powerful_arrays.txt +++ /dev/null @@ -1,210 +0,0 @@ -# ==================================================================================== -# Arrays -# ==================================================================================== - -# Arrays are incredibly powerful in Ruby. Learn to use them well. - -repl do - puts "* RUBY PRIMER: ARRAYS" -end - -# ==================================================================================== -# Enumerable ranges and .to_a -# ==================================================================================== - -repl do - puts "** INFO: Create an array with the numbers 1 to 10." - one_to_ten = (1..10).to_a - puts one_to_ten -end - -# ==================================================================================== -# Finding elements -# ==================================================================================== - -repl do - puts "** INFO: Finding elements in an array using ~Array#find_all~." - puts "Create a new array that only contains even numbers from the previous array." - - one_to_ten = (1..10).to_a - evens = one_to_ten.find_all do |number| - number % 2 == 0 - end - - puts evens -end - -# ==================================================================================== -# Rejecting elements -# ==================================================================================== - -repl do - puts "** INFO: Removing elements in an array using ~Array#reject~." - puts "Create a new array that rejects odd numbers." - - one_to_ten = (1..10).to_a - also_even = one_to_ten.reject do |number| - number % 2 != 0 - end - - puts also_even -end - -# ==================================================================================== -# Array transform using the map function. -# ==================================================================================== - -repl do - puts "** INFO: Creating new derived values from an array using ~Array#map~." - puts "Create an array that doubles every number." - - one_to_ten = (1..10).to_a - doubled = one_to_ten.map do |number| - number * 2 - end - - puts doubled -end - -# ==================================================================================== -# Combining array functions. -# ==================================================================================== - -repl do - puts "** INFO: Combining ~Array#find_all~ along with ~Array#map~." - puts "Create an array that selects only odd numbers and then multiply those by 10." - - one_to_ten = (1..10).to_a - odd_doubled = one_to_ten.find_all do |number| - number % 2 != 0 - end.map do |odd_number| - odd_number * 10 - end - - puts odd_doubled -end - -# ==================================================================================== -# Product function. -# ==================================================================================== - -repl do - puts "** INFO: Create all combinations of array values using ~Array#product~." - puts "All two-item pairs of numbers 1 to 10." - one_to_ten = (1..10).to_a - all_combinations = one_to_ten.product(one_to_ten) - puts all_combinations -end - -# ==================================================================================== -# Uniq and sort function. -# ==================================================================================== - -repl do - puts "** INFO: Providing uniq values using ~Array#uniq~ and ~Array#sort~." - puts "All uniq combinations of numbers regardless of order." - puts "For example: [1, 2] is the same as [2, 1]." - one_to_ten = (1..10).to_a - uniq_combinations = - one_to_ten.product(one_to_ten) - .map do |unsorted_number| - unsorted_number.sort - end.uniq - puts uniq_combinations -end - -# ==================================================================================== -# Example of an advanced array transform. -# ==================================================================================== - -repl do - puts "** INFO: Advanced chaining. Combining ~Array's ~map~, ~find_all~, ~sort~, and ~sort_by~." - puts "All unique Pythagorean Triples between 1 and 100 sorted by area of the triangle." - - one_to_hundred = (1..100).to_a - - triples = - one_to_hundred.product(one_to_hundred).map do |width, height| - [width, height, Math.sqrt(width ** 2 + height ** 2)] - end.find_all do |_, _, hypotenuse| - hypotenuse.to_i == hypotenuse - end.map do |triangle| - triangle.map(&:to_i) - end.uniq do |triangle| - triangle.sort - end.map do |width, height, hypotenuse| - [width, height, hypotenuse, (width * height) / 2] - end.sort_by do |_, _, _, area| - area - end - - triples.each do |width, height, hypotenuse, _| - puts "(#{width}, #{height}, #{hypotenuse})" - end -end - -# ==================================================================================== -# Example of an sorting. -# ==================================================================================== - -repl do - puts "** INFO: Implementing a custom sort function that operates on the ~Hash~ datatype." - - things_to_sort = [ - { type: :background, order: 1 }, - { type: :foreground, order: 1 }, - { type: :foreground, order: 2 } - ] - puts "*** Original array." - puts things_to_sort - - puts "*** Simple sort using key." - # For a simple sort, you can use sort_by - results = things_to_sort.sort_by do |hash| - hash[:order] - end - - puts results - - puts "*** Custom sort." - puts "**** Sorting process." - # for a more complicated sort, you can provide a block that returns - # -1, 0, 1 for a left and right operand - results = things_to_sort.sort do |l, r| - sort_result = 0 - puts "here is l: #{l}" - puts "here is r: #{r || "nil"}" - # if either value is nil/false return 0 - if !l || !r - sort_result = 0 - # if the type of "left" is background and the - # type of "right" is foreground, then return - # -1 (which means "left" is less than "right" - elsif l[:type] == :background && r[:type] == :foreground - sort_result = -1 - # if the type of "left" is foreground and the - # type of "right" is background, then return - # 1 (which means "left" is greater than "right" - elsif l[:type] == :foreground && r[:type] == :background - sort_result = 1 - # if "left" and "right"'s type are the same, then - # use the order as the tie breaker - elsif l[:order] < r[:order] - sort_result = -1 - elsif l[:order] > r[:order] - sort_result = 1 - # returning 0 means both values are equal - else - sort_result = 0 - end - sort_result - end.to_a - - puts "**** Sort result." - puts results -end - -# ==================================================================================== -# Api documention for Array that is worth commiting to memory because arrays are so -# awesome in Ruby: https://docs.ruby-lang.org/en/2.0.0/Array.html -# ==================================================================================== -- cgit v1.2.3