Skip to content

Instantly share code, notes, and snippets.

@leemour
Last active April 23, 2018 13:12
Show Gist options
  • Save leemour/0e4e11ff2774759601a0c8b1bd789f54 to your computer and use it in GitHub Desktop.
Save leemour/0e4e11ff2774759601a0c8b1bd789f54 to your computer and use it in GitHub Desktop.
Teaching Ruby classes
class Human
def initialize(name)
@name = name
end
def read_book(book, page_number)
page = book.open_page(page_number)
read(page)
end
def read(object)
puts "#{@name} reads: #{object}"
end
end
class Book
def initialize(text)
@text = text
end
def open_page(page_number)
@text[page_number]
end
end
text = [
"First page",
"Second page",
"Third page",
"Forth page",
]
book = Book.new(text)
name = 'Nikita'
boy = Human.new(name)
boy.read_book(book, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment