Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions 02.calendar/calendar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'date'

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

カレンダーのプログラムがPRに混じってしまっていますね。新しいブランチを作成する際は、今どのブランチにいるのかに注意して作成するようにしましょう。今回はこのままでOKです。

require 'optparse'

# コマンドラインでparseする
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: ruby calendar.rb [options]"

opts.on("-m", "--month MONTH", Integer, "Specify the month (1-12)") do |month|
options[:month] = month
end

opts.on("-y", "--year YEAR", Integer, "Specify the year") do |year|
options[:year] = year
end
end.parse!

# 今の日付を取る
current_date = Date.today
year = options[:year] || current_date.year
month = options[:month] || current_date.month

# date enumeratorを作る
date_range = Date.new(year, month, 1)..Date.new(year, month, -1)
dates_enum = date_range.to_enum

# カレンダーを表示する
puts "\n#{Date::MONTHNAMES[month]} #{year}".center(20)
puts "日 月 火 水 木 金 土"

# 最初の日に計算する
initial_offset = dates_enum.peek.wday

# 見やすいのため
print " " * initial_offset

# 1ヶ月分表示する
dates_enum.each do |date|
print date.day.to_s.rjust(2) + " "

# 土曜日になったら新しいラインを作る
puts if date.saturday?
end

puts "\n"

52 changes: 52 additions & 0 deletions 03.bowling/bowling.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'rubocop'
require 'debug'

score = ARGV[0]
scores = score.split(',')
shots = []

scores.each do |s|
if s == 'X'
shots << 10
shots << 0
else
shots << s.to_i
end
end

frames = []
shots.each_slice(2) do |s|

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここは以下のように書けますね。

frames = shots.each_slice(2).to_a

frames << s
end
p frames

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

デバッグ用だと思うので、削除しておきましょう。

point = 0

frames.each_with_index do |frame, index|

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここは以下のように Enumerable#sum を使うと、自分で point を加算しなくて良くなるはずです。

point = frames.each_with_index.sum do |frame, index|
  # 戻り値を各フレームのスコアにする
end

# binding.break
if index >=9
point += frame.sum
# elsif frame_index == 11

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要であれば削除しておきましょう。3行↑にある binding.break も同様です。


else
if frame[0] == 10
if frames[index + 1][0] == 10
if frames[index + 2][0] == 10
point += 30
else
point += 20 + frames[index + 2][0]
end

else
point += 10 + frames[index + 1].sum
end


elsif frame.sum == 10
point += 10 + frames[index + 1][0]
else
point += frame.sum
end
end
end

puts point