Skip to content
Open
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
72 changes: 72 additions & 0 deletions 02.calendar/calendar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#! /usr/bin/env ruby
require 'date'
require 'optparse'

# 入力データのチェック
def check_inputs(inputs)
year = inputs['y']
month = inputs['m']
if !year.nil? && year.to_i < 1873
puts '-yの引数には1873以上の数値を入力してください'
exit(1)
end
if !month.nil? && !month.to_i.between?(1,12)
puts '-mの引数には1から12の数値を入力してください'
exit(1)
end
end

inputs = ARGV.getopts('y:', 'm:')
check_inputs(inputs)
YEAR = inputs['y'].nil? ? Date.today.year : inputs['y'].to_i
MONTH = inputs['m'].nil? ? Date.today.month : inputs['m'].to_i
WEEKS = %w[Su Mo Tu We Th Fr Sa]
MONTHS = { 1 => 'January',
2 => 'February',
3 => 'March',
4 => 'April',
5 => 'May',
6 => 'June',
7 => 'July',
8 => 'August',
9 => 'September',
10 => 'October',
11 => 'November',
12 => 'December' }
# 出力する際に週と日付が同じ配列にあったほうが出力が楽であるため週と日を一緒にしてある
calendar = {:year => YEAR, :month => MONTHS[MONTH], :weeks_and_days => [WEEKS]}

def get_days(year, month)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

修正不要ですが、 ruby/rails界隈は get_xxx だと get_ 無くして xxx にする慣習があります。
とはいえ、その場合は取得するだけの簡単な場合に使う名前なので…僕だったら build_daysぐらいにするかなーと思います。前のgenerateでも良いと思います。

week_number = 1
# 呼び出す際にエラーにならないように空配列を入れてある
days = [[]]
first_day = Date.new(year, month, 1)
last_day = Date.new(year, month, -1)
(first_day.day..last_day.day).each do |day|
date = Date.new(year, month, day)
# 第1週はdaysのインデックス0に格納、のように週番号とインデックスがずれるため-1という処理を行っている
days[week_number - 1][date.wday] = day
if date.wday == 6
days.push([])
week_number += 1
end
end
return days
end

# カレンダーデータをもとにカレンダーを表示
def print_calendar(calendar)
printf('%8s', calendar[:month])
printf("%8s\n", calendar[:year])
calendar[:weeks_and_days].each do |row|
row.each do |cell|
printf('%3s', cell.to_s)
end
puts ''
end
end

get_days(YEAR, MONTH).each do |days|
calendar[:weeks_and_days].push(days)
end
print_calendar(calendar)