Last active
April 23, 2018 13:12
-
-
Save leemour/0e4e11ff2774759601a0c8b1bd789f54 to your computer and use it in GitHub Desktop.
Teaching Ruby classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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