I'm a beginner studying Rails. It's been about a month since I first learned rails. While I wanted to start learning Ruby from scratch, I arrived at the following article written by Mr. Ito, a Ruby programmer.
Today, I will refer to this and write about the code I wrote and what I learned in it. I would like to continue to brush up on my own, but I would be happy if other people could see it and point out.
calender.rb
require 'Date'
#Method to check the last day of this month
def last_day(year,month)
if month == 2 && Date.valid_date?(year,month,29)
return 29
elsif month == 2
return 28
elsif Date.valid_date?(year,month,31)
return 31
else
return 30
end
end
#See how many weeks this month is
def how_many_weeks(day_array)
days_count = day_array.length
if days_count / 7 == 5 && days_count % 7 != 0
return 6
elsif days_count / 7 == 5
return 5
elsif days_count / 7 == 4 && days_count % 7 != 0
return 5
else
return 4
end
end
#Generate today's date
today = Date.today
#Year from today's date/Month/Monthの英字名を出力する
year = today.year
month = today.month
name_of_month = today.strftime("%B")
#Find out the start day of the month
day_of_the_week = Date.new(year,month,1).wday
#Create an array of dates based on the last date
last_day = last_day(year,month)
day_array = [*1..last_day]
#Add a blank to the array for each start day
day_of_the_week.times do
day_array.unshift(" ")
end
#See how many weeks this month is
weeks_count = how_many_weeks(day_array)
#Creating a calendar title
calender_title = name_of_month + " " + year.to_s
#Calendar output
puts calender_title.center(21," ")
printf("%3s%3s%3s%3s%3s%3s%3s\n","Su","Mo","Tu","We","Th","Fr","Sa","Su")
weeks_count.times do
#Set to one week's content week
week = day_array.shift(7)
#As a countermeasure in the case of nil, leave a blank
if week.length < 7
e = week[6]
week.map {|e| e ? e : " " }
end
#Output one week's worth
printf("%3s%3s%3s%3s%3s%3s%3s\n",
week[0],week[1],week[2],week[3],week[4],week[5],week[6])
end
The output result is as follows this month
August 2020
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
printf("%3s%3s%3s%3s%3s%3s%3s\n","Su","Mo","Tu","We","Th","Fr","Sa","Su")
Indicates output as a string with % s
Specify the number of display digits with 3
in the meantime
There is a line break at the last \ n
.
By specifying the same format for the header part and the date, I was able to arrange it neatly.
Reference: Standard input function
puts calender_title.center(21," ")
Here, we are processing to have "" (blank) inserted so that the characters are in the center within the range of 21 which is the size of the calendar part. It's convenient!
Reference: String # center Ruby Reference
#Set to one week's content week
week = day_array.shift(7)
#As a countermeasure in the case of nil, leave a blank
if week.length < 7
e = week[6]
week.map {|e| e ? e : " " }
end
if week.length < 7
If the week
array with the date of the week has less than 7 elements
e = week[6]
By calling week [6], put nil in all the parts that have no value.
week.map {|e| e ? e : " " }
From there, use map to perform a ternary operation (replace with "" (blank) in the case of nil) and replace nil.
I think there is a better way, but ... I wanted to make it once, so I decided to do this.
Reference: How to manage nil of array and value so as not to cause an error in ruby
For the time being, I was able to make it very enjoyably through various trials and errors. As mentioned in the blog at the beginning, I think it is important to think more and make the code cleaner, not just to make it. Let's start by reviewing the code of this calendar creation app again and writing a test.
Once again, thank you to each site for your reference!
Recommended Posts