Hack hack hack...

An open journal-- some of it written for you, but most of it is for me.

FIS Day12

POTD: Tim Berners-Lee

Modules

Dogs and humans can both run, so should they inherit from runner or runnable?

We put this shared behavior into a module so that the class can include or mixin modules.

modules can include other modules

include - is for the instance methods extend - is for the class methods

super - call the same exact message on the class above

Simple Inheritance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Animal
 attr_accessor :legs

 def initialize(legs_count = 4)
  @leg = legs_count
 end
end

class Human < Animal
  attr_accessor :hair_color #this is an additional accessor
  #and doesn't over-ride any #other attr_accessor

#this overides the initialize in the Animal class
  def initialize
      super(2) #super sexy
  end

end
The Builder Pattern
1
2
3
4
5
6
7
8
class Animal
  include Movement::Animal
end

class Human
  include Movement::Animal
  include Movement::Human
end

A class is a module (that can be instantiated).

ERB

The standard library is separate because you can release ruby and the standard lib without updating the release of the other

<%= %> (known as a delimiter) is like interpolation for a template

1
2
3
4
5
6
7
8
9
10
11
12
require 'erb'

x = 42
#EOF = end of file
template = ERB.new <<-EOF
 the value of x is: <%= x %>
EOF

puts template.result(binding) #the value of x is :42

x = 24
puts template.result(binding) #the value of x is: 24

binding - is equal to the current scope - return to me everything in the universe that happened at this point in time.

Comments