Skip to content

Instantly share code, notes, and snippets.

@hyuki

hyuki/chat.md Secret

Last active May 19, 2023 06:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyuki/f976d09d6d63319d738d59ada6fe1b9f to your computer and use it in GitHub Desktop.
Save hyuki/f976d09d6d63319d738d59ada6fe1b9f to your computer and use it in GitHub Desktop.
#ChatGPT と結城浩の対話: 指定した日付以降にある、指定した曜日の日付を10週分出力するプログラムを作ってください。

Model: GPT-4

Date: 2023-05-19

Rubyで次のような機能を持つCLIを作成してください。コマンドラインからruby weeks.rb 2023-05-15 Friのように入力すると、2023年5月15日以降の毎週金曜日の日付を10週分表示するものです。実際のやりとりは次のようになります。

$ ruby weeks.rb 2023-05-15 Fri
2023-05-19
2023-05-26
2023-06-02
2023-06-09
2023-06-16
2023-06-23
2023-06-30
2023-07-07
2023-07-14
2023-07-21

Rubyでご要望のCLIを作成すると以下のようになります。このプログラムは、Dateクラスとwdayメソッドを使用して特定の曜日を探し、next_dayメソッドを使用して次の同じ曜日を見つけます。また、このプログラムはコマンドライン引数(ARGV)を受け取り、開始日と曜日を指定します。

require 'date'

start_date = Date.parse(ARGV[0])  # start date from command line argument
target_day = ARGV[1]             # target day from command line argument

# Map the days of the week to corresponding wday values
day_to_num = {
  "Sun" => 0,
  "Mon" => 1,
  "Tue" => 2,
  "Wed" => 3,
  "Thu" => 4,
  "Fri" => 5,
  "Sat" => 6
}

target_num = day_to_num[target_day]

# Find the first target day
until start_date.wday == target_num
  start_date = start_date.next_day
end

# Print the next 10 weeks of the target day
10.times do
  puts start_date
  start_date = start_date.next_day(7)  # Jump to the next week
end

このスクリプトは、次のように使用します。

$ ruby weeks.rb 2023-05-15 Fri

このコマンドは、2023年5月15日以降の毎週金曜日の日付を10週分表示します。

@hyuki
Copy link
Author

hyuki commented May 19, 2023

実行結果

$ ruby weeks.rb 2023-05-15 Fri
2023-05-19
2023-05-26
2023-06-02
2023-06-09
2023-06-16
2023-06-23
2023-06-30
2023-07-07
2023-07-14
2023-07-21

@hyuki
Copy link
Author

hyuki commented May 19, 2023

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