I tried to do it as an output of Ruby learning, To be honest, I didn't have any hands or legs, so I looked at the example answers and I decided to read it.
That record. I am studying in "Introduction to Ruby for those who want to become professionals" It seems that there is still not enough drop. .. ..
・ Example answer https://qiita.com/sackey_24/items/8fc236bb054aff6b74c8
・ Reference site https://www.javadrive.jp/ruby/date_class/index5.html
There were some things that I could understand by looking at the answers, and some things that I saw for the first time. I first looked around that and looked at it.
Either way, it doesn't seem to start without understanding the Date class. .. ..
-The Time class is a built-in library, but the Date class is different, so call it with "require". -The main methods are day, mouth, year, and wday (each is acquired as an integer. Wday is also acquired as an integer with Sunday as 0). -After creating an object, it can be converted to a character string using the format (strftime method). →% a Abbreviation for day of the week (sun, mon, thu, etc.)% w Number representing the day of the week
Now that I understand the Date class to some extent, I understand the meaning of the answer code.
First, create a variable
head = Date.today.strftime ("% B,% Y") # Get today's month and year → Display at the top of the calendar year = Date.today.year # This year's Christian era mon = Date.today.mon # Today's month
Now that you have created today's year and month from the Date class Created from Date class on the 1st and last day of this month as described in the requirements.
firstday_wday = Date.new(year,mon,1).wday
lastday_date = Date.new(year,mon,-1).day
week = %w(Su Mo Tu We Th Fr Sa)
The important point here is
*-Bring the above variable as an instance argument. ** ** ** ・ The last day can be obtained by writing "-1". ** ** ** ・% w is a notation that creates an array ( separate from the format character string) **
Next, actually output up to here.
puts head.center(20)
puts week.join(" ")
print " " * firstday_wday
Is it the same here? I will summarize where it became.
** ・ The center method has the number of arguments as the number of characters and is centered along with it ** ** ・ join ("") → The argument space is for displaying the days of the week in the week array with a sense of openness ** ** ・ Print displays blanks up to one day on the calendar using the characteristic of displaying without line breaks. ** ** ** → Make a space for the number of the day of the week **
Notation of the date at the end. Numbers are processed repeatedly, and line breaks are set by conditional branching.
wday = firstday_wday
(1..lastday_date).each do |date|
print date.to_s.rjust(2) + " "
Arrange the numbers right-justified. (Range specified by 1..lastday_wday) Then, from the last day of the week (1 if it is Monday), add 1 for each iterative process and start a new line when it reaches a multiple of 7.
wday = wday+1
if wday%7==0
print "\n"
end
end
if wday%7!=0
print "\n"
end
That's it.
May 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
Most of the parts were written in the reference book, but when it came time to implement it from 0, it became a slapstick. .. ..
By outputting it, you can check that part and the part that is not deeply talked about in the reference book (in this case, the details of the Date class), so I would like to continue steadily.
In addition, this article will be interpreted in my own way, so if there are any mistakes, I would appreciate it if you could point out. .. ..
Let's read back the chapter on arrays for the time being. .. ..
Recommended Posts