Skip to content

Instantly share code, notes, and snippets.

@davidjbeveridge
Created July 11, 2011 19:26
Show Gist options
  • Save davidjbeveridge/1076594 to your computer and use it in GitHub Desktop.
Save davidjbeveridge/1076594 to your computer and use it in GitHub Desktop.
Blocks and scope
class Book
attr_accessor :title
attr_accessor :author
attr_reader :chapters
attr_reader :pages
def initialize
@chapters = []
@pages = []
end
def add_page page
@pages << page
end
def mark_chapter title, startpage
@chapters << {:title => title, :startpage => startpage}
end
def toc
index = 0
@chapters.each do |chapter|
puts "Chapter #{++index}: #{chapter[:title]} page #{chapter[:startpage]}"
end
nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment