Skip to content

Instantly share code, notes, and snippets.

@VladHurma
Last active November 28, 2018 19:40
Show Gist options
  • Save VladHurma/29b9ab1ab5cce2c2faa4c802b629e7c7 to your computer and use it in GitHub Desktop.
Save VladHurma/29b9ab1ab5cce2c2faa4c802b629e7c7 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
class Goals_result
include Comparable
def initialize
@goals_scored = []
@goals_missed = []
end
def self.run
new.run
end
def run
initialize_games_results
say_user_games_results
end
private
def initialize_games_results
(0..19).each do |i|
@goals_scored[i] = rand(0..5)
@goals_missed[i] = rand(0..5)
end
end
def say_user_games_results
(0..19).each do |i|
print "#{i + 1}: "
check_up_game_result(@goals_scored[i], @goals_missed[i])
end
end
def check_up_game_result(score, miss)
if score == miss
show_user_result('Draw!')
elsif score > miss
show_user_result('Win!')
elsif score < miss
show_user_result('Lose!')
end
end
def show_user_result(result_by_words)
puts result_by_words
end
end
Goals_result.run
@aya-soft
Copy link

  1. Вот тут у метода очень много обязанностей (обход массива, сравнение значений, печать на экран, определение результата)
	def say_user_games_results
		for i in 0..19 do
			print "#{i+1}: "
			if @goals_scored[i] == @goals_missed[i]
				puts "Draw!"
			elsif @goals_scored[i] > @goals_missed[i]
				puts "Win!"
			else
				puts "Lose!"
			end
		end
	end

@aya-soft
Copy link

Вопросы к тебе:

  • какой в Руби есть оператор, который при сравнении выдает три значения?
  • можно ли здесь использовать case-конструкцию?

@aya-soft
Copy link

Раздели пожалуйста обязанности: обхода, вычисления результата и печать - в разные методы

@VladHurma
Copy link
Author

Вопросы к тебе:

* какой в Руби есть оператор, который при сравнении выдает три значения?

* можно ли здесь использовать case-конструкцию?

Comparable вроде(-1,0,1)
Можно

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment