Skip to content

Matrix Multiplication...

An ill fitting metaphor?

Matrix multiplication is not commutative.

This is very unintuitive, especially once they replace it with variables. At which point you get equations that look the same, but behave very differently.



To understand modules, we first have to understand a little bit about how Ruby works. So, lets define the things that exist in Ruby and see how they work together. Then, we will be able to understand how modules fit into this, and what they are here for.


View visualize_gol.rb
# ===== Game of Life visualizer =====
# You need to fill out these three methods
# and it will visualize your game
# input: an array of xy pairs, an xy pair is an array of 2 integers for a living cell
# output: whatever object you made to implement the game of life
def init(xy_pairs)
cells = do |y| { |x| xy_pairs.include? [x, y] }
View seeing_a_compiled_method.rb
# Classes store a hash of methods, the keys being the method names... but what are the values?
# Here you can see an example of what the code looks like when compiled.
# It's not important to understand how these instructions allow Ruby to work,
# but I want you to see that there is a concrete thing there that you can look at.
# (note, all of the `trace` instructions are irrelevant and can be ignored,
# their purpose is to allow you to hook into events from the outside
def greet
puts "hello world"
View example_high_level_app.rb
require 'active_record'
require 'erb'
require 'logger'
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
ActiveRecord::Base.logger = $stdout
ActiveSupport::LogSubscriber.colorize_logging = false
ActiveRecord::Schema.define do
self.verbose = false
View estimate_pi.rb
# solution for
require 'set'
require 'chunky_png'
# load the image from the data segment
raw_png ="\n").unpack('m0').first
image = ChunkyPNG::Image.from_blob(raw_png)
# extract the colours
colours = image.pixels.uniq
View rails_engine.rb
require 'active_record'
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
ActiveRecord::Schema.define do
self.verbose = false
create_table "invoice_items" do |t|
t.integer "invoice_id"
t.integer "quantity"

Module 4 Object Model with 1502!


Some definitions

  • Containers Hashes whose keys are Symbols
  • Pointers Fixnum whose value is a location in memory
View sorted_merging.rb
# I accidentally closed the window I wrote the solution in,
# so I couldn't find it, but I rewrote it. I shortened the names,
# so that I could get some of the ideas downt o one line.
# Also, thought of a way cooler way to implement our `smallest` method :)
def smallest(one, two)
[one, two].min_by(&:first)
View balanced_bst.rb
class BalancedBst
module Empty
extend self
def <<(data) data
def >>(data)
Something went wrong with that request. Please try again.