Created
November 28, 2018 08:47
-
-
Save axtutuu/13d745b8405729209514d91b0052a317 to your computer and use it in GitHub Desktop.
classroom.rb
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
class ClassRoom | |
def initialize | |
@students = [] | |
end | |
def save name, gender | |
@students.push( | |
{ name: name, gender: gender } | |
) | |
end | |
def students | |
@students | |
end | |
def mans | |
@students.select { |s| s[:gender] == "man" } | |
end | |
def womans | |
@students.select { |s| s[:gender] == "woman" } | |
end | |
end | |
room = ClassRoom.new | |
while true do | |
puts "選択してください" | |
puts "[0] 生徒を登録" | |
puts "[1] 生徒数/割合を取得" | |
input = gets.chomp | |
if input == "0" | |
puts "名前を入力" | |
name = gets.chomp | |
puts "性別を入力 { man or woman }" | |
gender = gets.chomp | |
room.save(name, gender) | |
end | |
if input == "1" | |
puts "生徒数は#{room.students.length}人です" | |
puts "男子の割合は#{(room.mans.length.to_f / room.students.length) * 100}%です" | |
puts "女子の割合は#{(room.womans.length.to_f / room.students.length) * 100}%です" | |
break | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment