Skip to content

カレンダープログラムの作成#2

Open
1mori wants to merge 13 commits intomainfrom
my-calendar
Open

カレンダープログラムの作成#2
1mori wants to merge 13 commits intomainfrom
my-calendar

Conversation

@1mori
Copy link
Copy Markdown
Owner

@1mori 1mori commented May 10, 2024

No description provided.

Comment thread 02.calendar/cal.rb Outdated
else
print "\n"
end
end No newline at end of file
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

細かいですが、末尾改行しましょうー

Comment thread 02.calendar/cal.rb Outdated
require 'optparse'

# 今日の日付を取得する
today_date = Date.today
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

細かいですが、=の後ろは半角空白1つにしましょう

Comment thread 02.calendar/cal.rb Outdated

# 今日の日付を取得する
today_date = Date.today
date_params = {year: today_date.year, month: today_date.month, day: today_date.day}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

細かいですが、{}の前後空白あけたいですねー

Comment thread 02.calendar/cal.rb Outdated
require 'date'
require 'optparse'

# 今日の日付を取得する
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

説明的なコメントは原則なくしたいですね。
参考
https://qiita.com/jnchito/items/f0d90af4ed44b7484103

Comment thread 02.calendar/cal.rb
opt.on('-y [VAL]') {|v| v.to_i}
opt.on('-m [VAL]') {|v| v.to_i}

opt.parse!(ARGV, into: cmd_params)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

into知らなかったです。便利ですねー 👍

Comment thread 02.calendar/cal.rb
else
date_params[:month] = cmd_params[:m]
end
end
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

後述してますが、今日の判定を見直すとsame_ymなくせそうですね。するとこの辺のロジックもすっきりしそうだな、と思います。

Comment thread 02.calendar/cal.rb Outdated
#色の反転
def reverse_cmpcolor(text)
"\e[7m#{text}\e[0m"
end
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

他のところメソッド化してないのにここだけ急にメソッドなことに違和感を感じます。なぜここだけメソッドなのですか?

Comment thread 02.calendar/cal.rb Outdated

# カレンダー部分の出力
# 余白部分の空白を予め出力しておく
dow_num = first_date.wday
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

dow_numがよくわからない命名だなと思いました。dowってなんですか? date of week? 一般的な略称なんでしょうか? あまり一般的でないなら変に省略すると可読性が下がるかな、と思います。

Comment thread 02.calendar/cal.rb Outdated
# カレンダー部分の出力
# 余白部分の空白を予め出力しておく
dow_num = first_date.wday
dow_num.times { print " " }
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

必須対応ではないですが、文字列で掛け算をするとすっきりかけそうです。

Comment thread 02.calendar/cal.rb Outdated
dow_num.times { print " " }

# 日にちの出力
(first_date.day..last_date.day).each do |num|
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ブロック変数にもっと具体的な名前をつけましょう。numではなんのnumなのかわからないですよね。

Comment thread 02.calendar/cal.rb Outdated
if same_ym == true && num == date_params[:day]
print reverse_cmpcolor(num.to_s.rjust(2))
(first_date..last_date).each do |current_date|
if current_date.year == date_params[:year] && current_date.month == date_params[:month] && current_date.day == date_params[:day]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

today_dateせっかくあるのでうまく活用しましょうー。Date型同士で比較するとシンプルですね

Comment thread 02.calendar/cal.rb Outdated
print current_date.day.to_s.rjust(2)
end
if dow_num == 6
if day_of_week_index == 6 # 曜日リセット
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

https://docs.ruby-lang.org/ja/latest/method/Date/i/saturday=3f.html というのが全曜日分あったりします。このへんうまく使うとday_of_week_indexは無くせるのではないかな、と思いました。いかがでしょうか。

Comment thread 02.calendar/cal.rb Outdated

# 日にちの出力
(first_date..last_date).each do |current_date|
if current_date.year == today_date.year && current_date.month == today_date.month && current_date.day == today_date.day
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Date型同士っていうのはこういうことじゃないです。これだと年(Integer)、月(Integer), 日(Integer)それぞれで比較ですよね。

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants