This commit is contained in:
julien committed 2026-10-26 09:52:44 +01:00
1 parent 1736f62660
commit 3f7a3a5f98
1 file changed
+61
+61
View File
@@ -0,0 +1,61 @@
#!/usr/bin/env ruby
class PeopleDirectory
def initialize(people)
@people = people
end
def find_people(name)
@people.select { |person| person[:name] == name }
end
end
class UserInput
def ask_name
print "De quelle personne souhaites-tu connaître l'âge ? "
gets.chomp
end
end
class PeopleView
def hello_everybody(people)
people.each do |person|
puts "Hello #{person[:name]}!"
end
end
def show_people(people)
if people.empty?
puts "Aucune personne trouvée."
else
people.each do |person|
puts "#{person[:name]} qui a #{person[:age]} ans"
end
end
end
end
# Données
people = [
{ name: "Alice", age: 30},
{ name: "Bob", age: 27},
{ name: "Alice", age: 22}
]
# Instancier les classes
directory = PeopleDirectory.new(people)
input = UserInput.new
view = PeopleView.new
# Saluer tout le monde
view.hello_everybody(people)
# Demander un nom
name = input.ask_name
# Rechercher
matches = directory.find_people(name)
# Afficher
view.show_people(matches)