diff options
| author | Amir Rajan <[email protected]> | 2020-07-26 16:31:07 -0500 |
|---|---|---|
| committer | Amir Rajan <[email protected]> | 2020-07-26 16:31:18 -0500 |
| commit | 8d000345a3489988e9e86ee9fda9dcc6c70b7012 (patch) | |
| tree | f4bed69140debb2aa007fe909083b5a461739c4b /samples/00_intermediate_ruby_primer | |
| parent | 958daf64294a3ed046d1e6737ac399e419e4ff36 (diff) | |
| download | dragonruby-game-toolkit-contrib-8d000345a3489988e9e86ee9fda9dcc6c70b7012.tar.gz dragonruby-game-toolkit-contrib-8d000345a3489988e9e86ee9fda9dcc6c70b7012.zip | |
added samples to public repo.
Diffstat (limited to 'samples/00_intermediate_ruby_primer')
9 files changed, 498 insertions, 0 deletions
diff --git a/samples/00_intermediate_ruby_primer/app/01_commenting_code.txt b/samples/00_intermediate_ruby_primer/app/01_commenting_code.txt new file mode 100644 index 0000000..37cd3ed --- /dev/null +++ b/samples/00_intermediate_ruby_primer/app/01_commenting_code.txt @@ -0,0 +1,13 @@ +# ==================================================================================== +# 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 diff --git a/samples/00_intermediate_ruby_primer/app/02_printing_to_the_console.txt b/samples/00_intermediate_ruby_primer/app/02_printing_to_the_console.txt new file mode 100644 index 0000000..54fbb5c --- /dev/null +++ b/samples/00_intermediate_ruby_primer/app/02_printing_to_the_console.txt @@ -0,0 +1,14 @@ +# ==================================================================================== +# 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. + +puts '' +puts '' +puts '================================' +puts 'Hello World' +puts '================================' +puts '' +puts '' diff --git a/samples/00_intermediate_ruby_primer/app/03_types_and_assignments.txt b/samples/00_intermediate_ruby_primer/app/03_types_and_assignments.txt new file mode 100644 index 0000000..2617ae4 --- /dev/null +++ b/samples/00_intermediate_ruby_primer/app/03_types_and_assignments.txt @@ -0,0 +1,74 @@ +# ==================================================================================== +# Types and Assignments +# ==================================================================================== +# +# Here is how you define various data structures in Ruby. Same deal here. Take the text +# in this file and paste it into repl.rb and save: + +puts '' +puts '' +puts '================================' +puts '' + +# ==================================================================================== +# Strings +# ==================================================================================== + +puts '======== 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.' +puts '' + +# ==================================================================================== +# Numerics +# ==================================================================================== + +puts '======== ints 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 '' + +# ==================================================================================== +# Booleans +# ==================================================================================== + +puts '======== 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}." + +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}." + +if !e + puts "This if statement ran because e is nil (a falsey value)." +end + +puts '' +puts '================================' +puts '' +puts '' diff --git a/samples/00_intermediate_ruby_primer/app/04_conditionals.txt b/samples/00_intermediate_ruby_primer/app/04_conditionals.txt new file mode 100644 index 0000000..6cf24f0 --- /dev/null +++ b/samples/00_intermediate_ruby_primer/app/04_conditionals.txt @@ -0,0 +1,104 @@ +# ==================================================================================== +# Conditionals +# ==================================================================================== + +puts '' +puts '' +puts '================================' +puts '' + +i_am_true = true +i_am_nil = nil +i_am_false = false +i_am_hi = "hi" + +# ==================================================================================== +# if +# ==================================================================================== + +puts "======== if statement" +i_am_one = 1 +if i_am_one + puts "This was printed because i_am_one is truthy." +end + +# ==================================================================================== +# if/else +# ==================================================================================== + +puts "======== if/else statement" +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 + +# ==================================================================================== +# if/elsif/else +# ==================================================================================== + +puts "======== if/elsif/else statement" +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 + +# ==================================================================================== +# case +# ==================================================================================== + +puts "======== case statement " +i_am_one = 1 +case i_am_one +when 10 + puts "10" +when 9 + puts "9" +when 5 + puts "5" +when 1 + puts "1" +else + puts "Value wasn't cased." +end + +# ==================================================================================== +# comparison operators +# ==================================================================================== + +puts "======== different types of comparisons" +if 4 == 4 + puts "equal" +end + +if 4 != 3 + puts "not equal" +end + +if 3 < 4 + puts "less than" +end + +if 4 > 3 + puts "greater than" +end + +# ==================================================================================== +# and/or conditionals +# ==================================================================================== + +if ((4 > 3) || (3 < 4) || false) + puts "or statement" +end + +if ((4 > 3) && (3 < 4)) + puts "and statement" +end + +puts '' +puts '================================' +puts '' +puts '' diff --git a/samples/00_intermediate_ruby_primer/app/05_looping.txt b/samples/00_intermediate_ruby_primer/app/05_looping.txt new file mode 100644 index 0000000..fafdcfc --- /dev/null +++ b/samples/00_intermediate_ruby_primer/app/05_looping.txt @@ -0,0 +1,61 @@ +# ==================================================================================== +# Looping +# ==================================================================================== +# +# Looping looks a whole lot different than other languages. +# But it's pretty awesome when you get used to it. + +puts '' +puts '' +puts '================================' +puts '' + +# ==================================================================================== +# times +# ==================================================================================== + + +puts "times block:" +3.times do |i| + puts i +end +puts '' + + +# ==================================================================================== +# ranges +# ==================================================================================== + +puts "range block exclusive:" +(0...3).each do |i| + puts i +end +puts '' + +puts "range block inclusive:" +(0..3).each do |i| + puts i +end +puts '' + +# ==================================================================================== +# Enumerables +# ==================================================================================== + +puts 'array each' +colors = ["red", "blue", "yellow"] +colors.each do |color| + puts color +end +puts '' + +puts 'array each_with_index' +colors = ["red", "blue", "yellow"] +colors.each_with_index do |color, i| + puts "#{color} at index #{i}" +end + +puts '' +puts '================================' +puts '' +puts '' diff --git a/samples/00_intermediate_ruby_primer/app/06_functions.txt b/samples/00_intermediate_ruby_primer/app/06_functions.txt new file mode 100644 index 0000000..6ee8da1 --- /dev/null +++ b/samples/00_intermediate_ruby_primer/app/06_functions.txt @@ -0,0 +1,49 @@ +# ==================================================================================== +# Functions +# ==================================================================================== + +# The last statement of a function is implictly returned. Parenthesis for functions +# are optional as long as the statement can be envaluated disambiguously. + +puts '' +puts '' +puts '================================' +puts '' + +# ==================================================================================== +# Functions single parameter +# ==================================================================================== + +def add_one_to n + n + 1 +end + +puts add_one_to(3) + +# ==================================================================================== +# Functions with default parameter values +# ==================================================================================== + +def function_with_default_value v = 10 + v * 10 +end + +puts "passing three: #{function_with_default_value(3)}" +puts "passing nil: #{function_with_default_value}" + +# ==================================================================================== +# Nil default parameter value and ||= operator. +# ==================================================================================== + +def function_with_nil_default_with_local a = nil + result = a + result ||= "or equal operator was exected and set a default value" +end + +puts "passing 'hi': #{function_with_nil_default_with_local 'hi'}" +puts "passing nil: #{function_with_nil_default_with_local}" + +puts '' +puts '================================' +puts '' +puts '' diff --git a/samples/00_intermediate_ruby_primer/app/07_powerful_arrays.txt b/samples/00_intermediate_ruby_primer/app/07_powerful_arrays.txt new file mode 100644 index 0000000..2c7e2f1 --- /dev/null +++ b/samples/00_intermediate_ruby_primer/app/07_powerful_arrays.txt @@ -0,0 +1,171 @@ +# ==================================================================================== +# Arrays +# ==================================================================================== + +# Arrays are incredibly powerful in Ruby. Learn to use them well. + +puts '' +puts '' +puts '================================' +puts '' + +# ==================================================================================== +# Enumerable ranges and .to_a +# ==================================================================================== + +puts "Create an array with the numbers 1 to 10." +one_to_ten = (1..10).to_a +puts one_to_ten + +# ==================================================================================== +# Finding elements +# ==================================================================================== + +puts "Create a new array that only contains even numbers from the previous array." +evens = one_to_ten.find_all do |number| + number % 2 == 0 +end +puts evens + +# ==================================================================================== +# Rejecting elements +# ==================================================================================== + +puts "Create a new array that rejects odd numbers." +also_even = one_to_ten.reject do |number| + number % 2 != 0 +end +puts also_even + +# ==================================================================================== +# Array transform using the map function. +# ==================================================================================== + +puts "Create an array that doubles every number." +doubled = one_to_ten.map do |number| + number * 2 +end +puts doubled + +# ==================================================================================== +# Combining array functions. +# ==================================================================================== + +puts "Create an array that selects only odd numbers and then multiply those by 10." +odd_doubled = one_to_ten.find_all do |number| + number % 2 != 0 +end.map do |odd_number| + odd_number * 10 +end +puts odd_doubled + +# ==================================================================================== +# Product function. +# ==================================================================================== + +puts "All combination of numbers 1 to 10." +all_combinations = one_to_ten.product(one_to_ten) +puts all_combinations + +# ==================================================================================== +# Uniq and sort function. +# ==================================================================================== + +puts "All uniq combinations of numbers." +puts "For example: [1, 2] is the same as [2, 1]." +uniq_combinations = + one_to_ten.product(one_to_ten) + .map do |unsorted_number| + unsorted_number.sort + end.uniq +puts uniq_combinations + +# ==================================================================================== +# Example of an advanced array transform. +# ==================================================================================== + +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 + +puts '' +puts '================================' +puts '' +puts '' + +# ==================================================================================== +# Example of an sorting. +# ==================================================================================== + +things_to_sort = [ + { type: :background, order: 1 }, + { type: :foreground, order: 1 }, + { type: :foreground, order: 2 } +] + +# For a simple sort, you can use sort_by +results = things_to_sort.sort_by do |hash| + hash[:order] +end + +puts "Simple sort:" +puts results + +# 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 "Custom sort:" +puts results + +# ==================================================================================== +# 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_intermediate_ruby_primer/app/main.rb b/samples/00_intermediate_ruby_primer/app/main.rb new file mode 100644 index 0000000..96461aa --- /dev/null +++ b/samples/00_intermediate_ruby_primer/app/main.rb @@ -0,0 +1,3 @@ +def tick args + args.outputs.labels << [640, 380, "Open repl.rb in the text editor of your choice and follow the document.", 0, 1] +end diff --git a/samples/00_intermediate_ruby_primer/license-for-sample.txt b/samples/00_intermediate_ruby_primer/license-for-sample.txt new file mode 100644 index 0000000..100dcec --- /dev/null +++ b/samples/00_intermediate_ruby_primer/license-for-sample.txt @@ -0,0 +1,9 @@ +Copyright 2019 DragonRuby LLC + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
