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
1mori
wants to merge
13
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.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8e576ed
本日の日付取得とコマンドライン引数の値を取得するコードを実装
1mori e4a992d
command_paramsの引数をcmd_paramsに名称変更
1mori 9f2a29a
cmd_paramsへの変更をcommand_paramsを使っていた箇所に反映
1mori df28d2b
コマンドライン引数から入力された数値をdate_paramsに上書きするように変更
1mori d8bc34e
読み込む引数の間違いを修正
1mori 72cfae4
カレンダーの表示部分のコードを作成
1mori eb8e1e1
今日の日付の出力結果の色をを反転させるプログラムを作成
1mori f340860
いただいたフィードバックの反映
1mori fffae61
Revert "いただいたフィードバックの反映"
1mori c96d4ef
Reapply "いただいたフィードバックの反映"
1mori f59cd38
date_paramsを削除し、今日の日付の取得をtoday_dateで行うように変更
1mori 9f7186e
day_of_week_indexを削除し、土曜日の判定にsaturday?を使用
1mori 3739d1c
今日の日付の判定方法をDateクラス同士で判定するように変更
1mori 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,56 @@ | ||
| #!/usr/bin/env ruby | ||
|
|
||
| require 'date' | ||
| require 'optparse' | ||
|
|
||
| today_date = Date.today | ||
|
|
||
| opt = OptionParser.new | ||
| cmd_params = {} | ||
|
|
||
| opt.on('-y [VAL]') {|v| v.to_i} | ||
| opt.on('-m [VAL]') {|v| v.to_i} | ||
| opt.parse!(ARGV, into: cmd_params) | ||
|
|
||
| target_params = { year: today_date.year, month: today_date.month } | ||
|
|
||
| if cmd_params[:y] # 年が指定されているとき | ||
| if cmd_params[:y] < 1970 || 2100 < cmd_params[:y] | ||
| raise OutOfRangeError, "年数が範囲外です。1970年から2100年までの年数を指定してください。" | ||
| end | ||
| target_params[:year] = cmd_params[:y] | ||
| end | ||
|
|
||
| if cmd_params[:m] # 月が指定されているとき | ||
| if cmd_params[:m] < 1 || 12 < cmd_params[:m] | ||
| raise InvalidRangeError, "正しい月を指定してください。" | ||
| end | ||
| target_params[:month] = cmd_params[:m] | ||
| end | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 後述してますが、今日の判定を見直すと |
||
|
|
||
| first_date = Date.new(target_params[:year], target_params[:month], 1) | ||
| last_date = Date.new(target_params[:year], target_params[:month], -1) | ||
|
|
||
|
|
||
| # カレンダーの表示部分を作成する | ||
| # x月 xxxxの出力、曜日の出力 | ||
| puts " #{target_params[:month]}月 #{target_params[:year]}" | ||
| puts "日 月 火 水 木 金 土" | ||
|
|
||
| # 1日目までの左上の空白を出力 | ||
| first_date.wday.times { print " " * 3 } | ||
|
|
||
| # 日にちの出力 | ||
| (first_date..last_date).each do |current_date| | ||
| if current_date == today_date | ||
| print "\e[7m#{current_date.day.to_s.rjust(2)}\e[0m" # 今日の日付の色を反転 | ||
| else | ||
| print current_date.day.to_s.rjust(2) | ||
| end | ||
| if current_date.saturday? # 曜日リセット | ||
| print "\n" | ||
| else | ||
| print " " | ||
| end | ||
| end | ||
| print "\n" unless last_date.saturday? # 最後の行の改行 | ||
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.
into知らなかったです。便利ですねー 👍