forked from fjordllc/ruby-practices
-
Notifications
You must be signed in to change notification settings - Fork 0
カレンダープログラムを作成しました。 #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
s-tone-gs
wants to merge
12
commits into
main
Choose a base branch
from
my-calendar
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+72
−0
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6af292b
first commit
s-tone-gs 1d4db41
コードを読みやすいように修正
s-tone-gs 0093e75
カレンダーの年月の表示がcalコマンドと違ったので修正
s-tone-gs 32ed99d
プロセスを終了するという意図を明確に示すために修正
s-tone-gs b62599b
Dateオブジェクトの再利用性を上げるために修正
s-tone-gs ee3f428
冗長だったため削除
s-tone-gs 9c92cfa
メソッドが何をしているかを分かりやすくするために修正
s-tone-gs a2e05e6
変数の中身が分かりやすいように修正
s-tone-gs e8fd32e
意図しない再代入が起こらないように修正
s-tone-gs 85880ea
メソッドを単一責任にするため修正
s-tone-gs ccb4af4
デバッグの記述を削除
s-tone-gs 752e150
処理の流れを自然にするために修正
s-tone-gs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| 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) | ||
| 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) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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でも良いと思います。