Pickaxe Book Chapter 05

This chapter is about inheritance, modules and mixins.

To make a subclass:

class Child < Parent

You can invoke parent class methods with “super”, just like in Java. You will usually want to call “super” in the initialize method if you are inheriting.

On page 95, it would be nice if the “private” keyword was tabbed in a few spaces.

Modules provide namespaces and allow some reusability. But unlike packages in Java it seems like they are similar to Ruby classes with two differences:
1. They have the word “module” in front of the name instead of the word “class”
2. Methods defined in the module have the module name in front of them. So if a Trigonometry class had a method “sin(x)” tyou would see this:

class Trigonometry
    # This is a constant
    PI = 22/7
    def sin(x)
        # implementation
    # other stuff

A module would do it like this:

module Trigonometry
    def Trigonometry.sin(x)
        # implementation
    # other stuff

If you use a module constant, you need to specify the module name and use the double-colon operator:

y = Trigonometry.sin(Trigonometry::PI/2)

Modules cannot have instances, so I guess they are like static classes in Java.

Is there inheritance for modules? I may find out

A mixin is when you include a module in a class. Then you can call the module’s method from a class instance as if they were parent methods.

The book talks quite a bit about the Enumerable mixin. I guess all you have to do is provide an “each” method, and you get all the iteration methods in collections (Array and Hash).

At the end of the chapter, they recommend designing programs with composition (has-a) and delegation (uses-a) instead of inheritance (is-a). They seem to say that mixins are more of a composition technique than an inheritance technique.

Image from Wikimedia, assumed allowed under Fair Use. Image from the Ambrosian Iliad, a 5th century manuscript of the Iliad.

Pickaxe Book Chapter 04

This chapter starts out with arrays (square brackets) and hashes (curly braces), which are important to know. It says that blocks when combined with collections are good iterators. The elements in arrays and hashes can be any type. Using different methods, arrays can be different data structures: sets, queues, dequeues, FIFO queues, instead of using different classes. In Ruby hashes, it keeps the objects in the order they were entered. Keep the names of the block parameters unique. You could use a colon, but I think it is better to keep them unique.

A block can be empty:

def three_times
    puts "in method three_times"
    puts "leaving three_times"

three_times { } 
# { puts "Hello" }

But you must put in a block if a method has “yield” in it, even if yield has a parameter

To keep track of how many times you have gone through the loop, instead of each, use each_with_index. You will need a second variable for the block: index to track the count.

f = File.open("para.txt")
f.each_with_index do |line,index|
    puts "Line #{index} is: #{line}"

I did not quite get the “inject” method, and I skipped over some of the stuff about enumerators since it said I could.
Class methods start with the word “self” in front of them

At the end it gets into using blocks for closures and lambdas. Do we get into that with Rails? I think I get this stuff, but sometimes I have to look at it for a few minutes to get it. I don’t think I really know too well. Plus it appears there is another syntax for Ruby 1.9, which makes it more confusing.

There is an encouraging note at the end that if you don’t get it now, it’s okay. Thanks, Dave!

Image from Wikimedia, assumed allowed under Fair Use. Image from the Vatican Virgil, a 5th century manuscript of poems by Virgil.

Pickaxe Book Chapter 03

This chapter was pretty easy. Mostly about objects and variables.

So what is an attribute? It seems like it is another name for an instance variable. They start with an “at” symbol. I am still not too clear what a symbol is.

I did not pay too much attention to the part on protected methods. I read somewhere that Bjarne Stroustrup, the creator of C++, at one point said the “protected” keyword was a mistake. Ever since then I have not paid much attention to protected.

Pickaxe Book Chapter 02

Here are some notes on Chapter 2 of the Pickaxe book.


Single quoted strings take less processing, no interpolation
Return value of a method is the last statement, so you may not always want/need to put a “return” statement in there.
Local variables, method parameters and method names all start with lower case or underscore.
Global variables start with a dollar sign.
Instance variables start with an at symbol.
Class variables start with two at symbols.
Class names, module names and constants start with uppercase letter.
Character after @ may not be a digit
Multi-word instance variables get underscores between words, multiword class names get camel case.
Method names may end with ?, ! and =, although these have special meaning.

Arrays: like lists or arrays in Java, unlike primitive arrays you do not need to specify the size ahead of time.
Hashes: Like maps in Java.
The values do not need to be of the same type.
Shortcut for arrays:
a = [‘ant’, ‘bee’, ‘cat’, ‘dog’, ‘elk’]
same as
a = %w{ant bee cat dog elk}

Symbols: A lot of people in class had trouble with this concept. I am not too clear on it myself. They can be used as keys for hashes.
There is also a new Ruby 1.9 syntax for hashes.
Here is the old way:

inst_section = {
  :cello     = 'string',
  :clarinet  = 'woodwind',
  :violin    = 'string'

Now you can do this as well:

inst_section = {
  cello:    'string',
  clarinet: 'woodwind',
  violin:   'string'

This has caused some controversy on some Ruby mailing lists.
There are no braces for control structures. Instead Ruby uses the keyword “end”.
Next the book covers regular expressions. I am not going to get into details here. I have seen this before, although I know there is a lot to see.
Blocks are a bit weird to me. I will have to look them up somewhere. It seems to me like it is just another arg to a method. Is it special because it is code?
I altered some of the code from the book to make a method take a parameter as well as a block:

def call_block(arg)
  puts "Start of method"
  puts "arg is #{arg}"
  puts "End of method"

call_block('hello'){ puts "In the block" }
puts "About to call call_block with no block"
call_block('empty block'){}

The book says that blocks are used to implement iterators. I can never remember how to iterate through stuff. I will have to make a separate post about that.

cities = ['Chicago', 'Austin', 'Dallas' , 'Houston']
cities.each {|city| puts "The city is #{city}"}

3.upto()6 {|i| print i}
('a'..'e').each {|char| puts char}

The I/O section talks about printf. I thought the whole point of Ruby was not to deal with C?
puts prints to the terminal, gets gets from the terminal.
The command like arguments are in an array called ARGV.

Image from Wikimedia, assumed allowed under Fair Use. Image from the Vatican Virgil, a 5th century manuscript of poems by Virgil.

Pickaxe Book Chapter 01

I have finished the first chapter of the Pickaxe book. The first chapter was no big deal for me. It was about getting and installing Ruby and a few basic commands.

I have used rvm to install Ruby on my Ubuntu laptop, and Rails Installer to install Ruby and Rails on my Windows laptop.

Ruby Plans

One of my plans outside of Code Academy is to go through the Ruby “Pickaxe Book” by Dave Thomas. Someone gave me a copy of the edition for Ruby 1.9. I will write a short post about each chapter as I finish them.

Image of a ruby from Wikipedia, assumed allowed under Fair Use.