Feel Physics Backyard

HoloLensの出張授業をする会社で、教材を開発しています

090615-夜更かし防止スクリプトをRubyで書いてみた

新しい記事があります。

リンク: 090624-朝、家を出るまであと何分?を表示するrubyスクリプト: Non-programmer's Ruby in Pocket.

今回は、前回の機能に追加して、朝、家を出るまであと何分あるのかを表示するようにした。


どうも最近夜更かし気味なので、「今寝ると睡眠時間はどれくらいなのか」を表示するスクリプトRubyで書いてみた。実際にはタスクスケジューラで15分おきに走らせている。わりあい良い感じで動いているのでご紹介。

now = Time.now
# 起床時刻は翌日の6時30分
getup_time = Time.local(now.year, now.month, now.day + 1, 6, 30)
# 出発時刻は当日の7時30分
going_time = Time.local(now.year, now.month, now.day, 7, 30)

puts "現在時刻:"
puts now.strftime("%H:%M")
puts
sleep 1

class TimeLength
  def initialize(seconds)
    @seconds = seconds
  end
  def format
    minutes = (@seconds / 60).round
    hours = (minutes / 60).round
    minutes = minutes % 60
    return hours.to_s + " 時間 " + minutes.to_s + " 分"
  end
end

# 午前8時以前ならば
if now.hour <= 8
  puts "出発時刻まで:"
  # 当日の起床時刻までの時間を表示
  tl = TimeLength.new(going_time - now)
  puts tl.format
# 午前8時以降ならば
else
  puts "起床時刻まで:"
  # 翌日の起床時刻までの時間を表示
  tl = TimeLength.new(getup_time - now)
  puts tl.format
end

sleep 10