Skip to content

Instantly share code, notes, and snippets.

@rikas
Created April 17, 2019 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rikas/d1e5c3839d30196b30913e217df2d719 to your computer and use it in GitHub Desktop.
Save rikas/d1e5c3839d30196b30913e217df2d719 to your computer and use it in GitHub Desktop.
Livecode Basic OOP - 243
class Citizen
# attr_reader :first_name, :last_name, :age
attr_reader :age, :height
def initialize(first_name, last_name)
@first_name = first_name
@last_name = last_name
@age = 0
@height = rand(10..40)
@alive = true
end
def grows_older!
@age += 1
get_taller!
kill_citizen!
end
def can_vote?
@age >= 18
end
def dead?
!@alive
end
def full_name
"#{@first_name.capitalize} #{@last_name.capitalize}"
end
private
def get_taller!
if @age < 18
@height += rand(5..7)
elsif @age < 20
@height += rand(1..3)
end
end
def kill_citizen!
@alive = false if @age >= 100
return if @age < 50
if rand(@age..100) == 100
@alive = false
end
end
end
require_relative 'citizen'
citizen = Citizen.new('aman', 'bharti')
puts "The life of #{citizen.full_name}..."
while !citizen.dead?
citizen.grows_older!
end
sleep(2)
puts "RIP #{citizen.full_name} is dead! 😵"
puts "He was #{citizen.age} years old and #{citizen.height}cm"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment