Created
September 28, 2017 18:59
-
-
Save therod/59825c40898ac3c7d48fd10220770caa 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
require 'date' | |
require 'active_support/all' | |
class Person | |
attr_accessor :name, :birthdate, :favorite_color | |
def initialize(name, birthdate, favorite_color) | |
@name = name | |
@birthdate = birthdate | |
@favorite_color = favorite_color | |
end | |
def introduce_yourself! | |
"Hallo, mein name ist #{@name}, meine lieblingsfarbe ist: #{@favorite_color}!" | |
end | |
def age | |
today = Date.today | |
birthdate = Date.parse(@birthdate) | |
years = (today.year - birthdate.year) | |
birthday_this_year = birthdate + years.years | |
if birthday_this_year >= today | |
years - 1 | |
else | |
years | |
end | |
end | |
end | |
person1 = Person.new("Marion", "11.02.1980", "green") | |
person2 = Person.new("Katrin", "11.02.1970", "green") | |
puts person1.age | |
puts person2.introduce_yourself! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment