Created
September 4, 2019 07:39
-
-
Save woarewe/6c1029a087fa9347fdbdf278be5950b7 to your computer and use it in GitHub Desktop.
This file contains 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
# frozen_string_literal: true | |
class Human | |
# What does this mean? Who is owner of this instance variable? | |
@created_humans = [] | |
@@humans_count = 0 | |
# What does mean this instruction | |
class << self | |
attr_accessor :created_humans | |
end | |
# And this? | |
def self.humans_count | |
@@humans_count | |
end | |
def self.to_s | |
'Class Human' | |
end | |
attr_reader :name, :age | |
def initialize(name:, age:) | |
@name = name | |
@age = age | |
self.class.created_humans << self | |
@@humans_count += 1 | |
end | |
def to_s | |
"#{name} -> #{age}" | |
end | |
end | |
john = Human.new(name: 'John', age: 20) | |
max = Human.new(name: 'Max', age: 15) | |
barbara = Human.new(name: 'Barbara', age: 40) | |
# What about this | |
def john.to_s | |
"I'm John, and I'm #{age} years old" | |
end | |
puts john | |
puts max | |
puts barbara | |
puts Human | |
puts Human.humans_count | |
puts Human.created_humans |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment