Makers Pre-Course — Week 2

Chris Whitehouse
7 min readNov 6, 2020

A slightly dull, but comprehensive learning diary for Makers.

Photo by Joshua Fuller on Unsplash

Day 6 — Monday 2nd November

Feeling at beginning of day: excited

Weeks 2&3 learning objectives: be able to write real world procedural programmes.

Chapter #1: Programmes are Worlds

  • Two things to run ruby code: some code, an interpreter
  • Programme = World. Objects live in this world. Although the world is an object too. In ruby the ‘world object’ is called ‘main’.
  • irb: interactive ruby, is an REPL (read, evaluate, print, loop) programme, for interacting with ruby. Allows you to become an object in the ruby world, that can interact with other objects.
  • Procedure: a set of instructions, one after the other.

Chapter #2: Exploring the World

  • Objects: every object knows something about itself, and how to interact with other objects.
  • Integers: integers are objects that live in main. Each number knows how much it is worth, and how to interact with other numbers.
  • Naming: objects can be named by assigning a variable.
  • Objects that can change are called mutable. Objects that can not change are called constants. By definition, constant names are capitalised.
  • Modulo: the remainder from any integer division

Chapter #3: Messages and Interfaces

  • Objects can receive ‘messages’ and ‘return’ values.
  • Each object has an ‘interface’ or, ‘public instance methods’, that determine how that object interacts with other objects. This is a list of ‘methods’.
  • The methods an object can respond to determine what an object is.

Duck Typing: “if is walks like a duck, and quacks like a duck, it is a duck”

  • Dot (.) syntax: methods are applied to objects with the dot syntax.
  • Even arithmetic operators +,-,*,/ are actually methods e.g. 1.+(1)
  • Methods, can be passed arguments, that determine how they should be operated.
  • Methods can be chained; whereby methods are called on the return from an object. This possible due to ‘referential transparency’.
  • Every object has a method called ‘methods’: that lists all the methods that can be applied.

Feeling at beginning of day: distracted

Day 7 — Tuesday 3rd November

Feeling at beginning of day: impatient.

Learnt within day:

Flow Control

  • Messages call methods on an object’s interface.
  • Going inside methods: methods contain procedures for objects to do. Procedures can be thought of as coding recipes.
  • Conditional Procedures: conditional procedures depend on the outcome of a condition. A structure that branches is called a conditional. A statement to determine which branch to proceed down is called a condition.
  • Self is the object on which the method will be operated.
  • The process of replacing statements and names in referential transparency is the first key skill in computational thinking.
  • Logical Operators: true and false represent boolean values and are used to do logic in combination with ‘and’ (&&), ‘or’ (||), and ‘not’ (!).
  • Mathematical Operators: order: BODMAS (brackets, order, division, multiplication, addition, subtraction).
  • Repeating Instructions: while loop; repeats everything between do and end whilst a given condition is true. (If uses ‘then’, everything else uses ‘do’). In ruby, only one object can run procedures at a time.
  • Accumulators: keeping track of a loop status.
  • Break and return: nothing happens in the loop after each of these. Return provides a return value from the loop.
  • Random conditionals can be used to create random branch behaviour.

Other Objects:

  • Recap: naming is the process of assigning variables to objects. Instruct objects to do things using variables. Messages call methods on an object’s interface.
  • Nil is an object that represents nothing.
  • Classes; are the creators of all things. Right before our universe made the objects, it made the gods. In programming world we call gods; ‘classes’. Classes are objects, which create objects.
  • Integer class: 1,2, 5 etc are instances of the Integer class. Class names have the first letter capitalised. The process of asking classes to create instances is called instantiation.
  • String class: instances of the String class are called strings. Strings represent text.
  • Integer instances understand the value of numbers. String instances understand text. Integers know how to interact with other integers, strings know how to interact with strings. Each have methods, that may look similar but may not necessarily do the same thing e.g. ‘1 + 2 = 3’ and “1” + “2” = “12”
  • Main Object: The main object is a special kind of instance; an instance of the Object class. All instances of all classes are ultimately instances of the Object class.
  • Output: from output from objects can take the form of return to other objects, or puts to the standardOutput.
  • Input: similarly input to objects in the form of arguments and messages, or gets from the standardInput.

Feeling at end of day: slightly concerned i’m not making quicker progress.

Day 8 — Wednesday 4th November

Feeling at beginning of day: unsettled

Learnt in day: Advanced Flow Control

  • Recap: already met; integers, True&False, Nil, Floats, Strings. With the following tools: naming, statements, messages&methods, conditionals, while loops, strings, gets&puts.
  • First job as programmers is to be able to to break up specifications into manageable step by step instructions we can feed into the machine. The process of breaking specifications into requirements is called algorithmic thinking.
  • The best programmer is a lazy programmer..! The best programmers do everything they can to avoid breaking off more than they can chew.
  • Good code is not as terse as as possible. Good code fulfills the requirements as closely as possible.

Feeling at end of day: finding writing the text adventure games a little tedious.

Day 9 — Thursday 5th November

Feeling at beginning of day: pensive

Learnt within day: Arrays

  • We call whatever an object ‘contains’ that object’s state.
  • The Array class creates instances that can store many other objects inside themselves. Array instances are called arrays.
  • Arrays grow and shrink depending on how many objects they contain. Any object which can change its state during the course of a programme is called mutable. Strings are mutable, integers are not.
  • Use self in the main context to work with the main object.
  • Modifying arrays: add to an array with the push method.
  • Things inside an array are called elements.
  • We can delete an element from an array using the delete_at method, with an argument denoting the index.
  • Arrays are zero indexed.
  • We can remove just the last element of an array with the pop method.
  • You can read any single element within the array with the [#] method, with an index argument.
  • You can read a run of elements with the slice method.
  • Can join elements of an array into a string with the join method, and an argument to denote the join character(s).
  • Ruby can treat strings as arrays, with many of the same methods. And can convert a string into an array with the split method. Default split character is space “ “, but any string can be used.
  • An array can contain any kind of object, although it’s rare for an array to contain more than one type at a time.
  • The length method can used to discover how many elements are in an array.
  • Arrays can be iterated over, using the each method. A variable that is named on the fly in a procedure is called a parameter.
  • Arrays can be used as accumulators within loops to record states.

Test Tweets Challenge crux: split with one phrase and join with another, can be used as a quasi find and replace in strings.

Question 1 crux: strings can be multiplied by an integer to repeat.

Question 2 crux: Found two methods to cycle through an array; either the modulus arithmetic operator with a loop counter in combination with the size of the array, or the cycle method. (cycle method felt like cheating).

Question 3 frustration: read question detail carefully, particularly sequences and details; capitalisation and punctuation etc. crux; able to interrogate through an array and the underlying string with the [][] double inspection method.

OTHER: Be careful with slack threads.

Feeling at end of day: frazzled.

Day 10 — Friday 6th November

Feeling at beginning of day: tired

Learnt within day: Hashes

  • Arrays and hashes are similar in that they both contain a list of elements. The main difference is: array elements are single objects. Hash elements are pairs of objects, one used to reference the other. A Key-Value pair.
  • Arrays use indices as keys. Hashes can use any object as a key. Most commonly hash keys are symbols.
  • Symbols are a special and very widespread object in ruby. They work like strings except they are immutable. Since we rarely want to change the keys in a hash, symbols are the perfect choice.
  • Formal method of naming a symbol is with double quotes but syntactic sugar denotes these as unnecessary. Alternatively the .to_sym method can be called on a string.
  • First function of hashes is as a key-value store for named information. Sometimes this is called a dictionary, sometimes a lookup table.
  • A lookup table can be used to control the flow of information. They act as a neater version of a long if or case statement.
  • Things can be grouped in hashes. Such that the value leg of the key-value pair can be an array, with a group of related data.
  • Like arrays, we can iterate over hashes. However, when we do, we get the key-value pair returned. These parameters can be named anything, to syntactically the key is presented first, then the value.

Array of Hashes to Hash of Arrays: crux: methodical deconstruction of the process. Can use .has_key? method to check if the key has already been created. Can add to the underlying array with the .push method as per usual.

Question 1: crux: don’t have to define the key as a symbol. Can be an integer. it's no longer legitimate to convert a symbol to an integer in ruby.

Feeling at end of day: pumped! felt like I completed the last to questions well.

--

--