10-01-2022

1010 words 6 minutes

Recap

  • data is assigned to named variables defined within the programme
  • objects can be sent things using messages (as Smalltalk)
  • functions, objects.init, and methods take arguments - you send data into them
  • the set of methods associated with a specific object might define its "interface"
  • a message can be chained data.method1.function1.method2(3).function3(Age: 56, Name: Jerry)
    • Chaining is possible thanks to referential transparency
  • Messages call methods on an objects interface
  • messages or functions are called on data (another object, or a variable)

Procedures

Let's get me to make a cake

  1. Get bowl
  2. Add flour
  3. Add eggs
  4. Add sugar
  5. Bake mixture for 20 minutes
  6. Cool mixture
  7. Return mixture
bowl = []
bowl.push(flour)
bowl.push(eggs)
bowl.push(sugar)

mixture = bowl.contents
mixture.bake

return mixture

Conditional Procedures

If I tried to bake a cake without a bowl - maybe it would work? but it would make uneccesary problems

  • Get Bowl
  • If bowl exists add flour
  • if bowl exists add eggs
  • if bowl exists add sugar
  • error code, return 0 (is this the same as C?)

In Ruby, check a number is positive with the '.positive?' function/method?

.positive?

if self > 0
	return true
else
	return false
end

if the input were 1:

if 1 > 0
	return true
else
	return false
end

Each possibility of the above is valled a branch Since we're inside an object's method, we can use referential transparency to replace self with 1

the above evaluates to

if true
	return true
else
	return false
end
Remember that all conditional statements are boolean - must evaluate to either true or false / 1 or 0
AST - an abstract syntax tree
Abstract Syntax Trees are used for more than just language interpreters - it is a branching if-else statement - a complex tree of conditional relations

Ruby - elsif

the else-if conditon is called within a boolean loop using the elsif statement

if elsif elsif else end

Comparison Operators

\ == equal to careful in javascript use === instead of ==

javascript will do weird things and only compares like for like using === (triple equals)

!= not equal
<  less than
<= less than or equal to
>  greater than
>= greater than or equal to

Logical Operations

  • true
  • false
  • && and
  • || or
  • ! not
  • Ruby is a truthy language - nil is an object that is false,
    • but even 0, and an empty string "" return true beware

While loops are loop upon a boolean value

  • break statements will exit the loop
  • used in old games
while game on
	game.run
end
A game of chess might run as

`while true do player_1.play player_2.play

if checkmate || stalemate break end end `

when running while loops and accumulators, be very aware of the accumulators position in the procedure

Return

The return statement says to an object, 'stop executing this block' return the given value return x

variables, objects, even functions can have types

thus far we've seen that variables in Ruby can be of type
  • integer

  • float

  • true

  • false

  • what other objects are there?

  • nil - an object that represents nothing, and explicitly says "absence"

    • it, along with false, returns false, but it IS an object
  • objects

Classes

Classes are another kind of object

  • classes are like the Gods of Ancient Greece
  • in Ruby, 1, 2, and 5 are instances of the Integer class
  • if we already have a class (kind of like a blueprint or a score) we can instantiate that object, often with the .new() method Class.new()
  • in irb, a new object will print out its memory address in hexcode ( pointer )
  • Ruby contains the String class
  • this would kind of allow for some type-testing at initialisation in Ruby
  • todo_1 = String.new("wash the dog")
  • todo_1.capitalize
  • !! classes are NOT instances
  • classes produce instances of objects
  • instance objects DO stuff

ruby uses case x when x-is-something logic when x-is-something-else logic when x-is-something-completely-different logic end

chapter 6

  • recieve a specification

    • ( what does the user want? )
  • break spec down into smaller steps

  • remember that Ruby chomp removes the '\n' char

chapter 7 arrays and iterators

  • an integer type contains the value of a variable
  • a string contains text - an array of characters
  • the contents of an object, or data, are described as its state
  • how about an object that can contain more than one thing?
  • Array instances are called arrays
    • an object that can change its state is mutable - as opposed to an immutable object
  • Ruby "arrays" can contain multiple types ( is it an array of pointers like in Python? )
  • Arrays can use the .push() notation to add an item to the end of the array
  • Arrays can be initialised with elements using the ["this". "is", "an", "array"] syntax
  • Arrays items can be deleted_at(index)
  • Like a standard array, the .pop() method will return the last element of the array
  • to return the first element without modifying the array (sort of like returning a window or small slice) the array[2] syntax can be used
  • the array.slice() method can be used to take a number of elements from an array
  • must look into the join method on the arrays, usually will concatenate an array of strings, using the argument within the join function .join(" ")
  • because strings are arrays of characters, a number of array methods will also work on strings
  • the split method can be called onto strings - without a parameter, the split method will divide the string up by its space characters, and then remove the space, returning an array of individual strings
  • split can be called on any other single item within a string
  • to divide a string up into each and every character, string.split(")
  • a good example of string modification through chaining bad_string = "Why|am|I|so|hard|to|read"

bad_string.split("|").join(" ") "Why am I so hard to read"

  • In Ruby, supposedly it is rare for an array to have more than one type

  • an array can contain an array of bools, integers, strings or even objects

  • an array can also contain an array of strings - a nested array

    • these can be known as 2D, 3D, 4D arrays depending on how many arrays they have
  • arrays can be combined together, almost as string concatenation

  • the method array.length will return the number of elements in the array

  • when a procedure is conducted on each element in an array, we are iterating through the array

  • a rubyist way of iteration is using the

array.each do
	logic
end

type of logic

to self reference each element, use a closure

array.each do |element|
	logic to element
end