Creating a calendar using Ruby

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

Read by code

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. .. ..

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     

Looking back

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

Creating a calendar using Ruby
Creating a 2021 weekly calendar (refill for personal organizer) using Ruby
Create a fortune using Ruby
[Android] Create a calendar using GridView
I tried a calendar problem in Ruby
Creating a user authentication function using devise
Escape processing when creating a URL in Ruby
Creating a browser automation tool with Ruby + Selenium
(Ruby on Rails6) Creating data in a table
[Rails] Creating a breadcrumb trail using Gem gretel
[For beginners] Procedure for creating a controller using rails
Creating a local repository
Creating a test case
[Ruby] Cut out a string using the slice method
What is a safety reference operator (&.) Using Ruby ampersand?
Creating a Cee-lo game with Ruby 4th Creating a game progress process
A memorandum for creating an extended logger using org.slf4j.Logger
Ruby Learning # 2 Drawing a Shape
What is a Ruby module?
Creating a batch of Liferay
Try using Cocoa from Ruby
Creating a Scala custom ExecutionContext
[Rails] Creating a search box
Ruby Learning # 11 Building a Calculator
Statically typed Ruby using Sorbet
Multiplication in a Ruby array
Procedure for publishing an application using AWS (4) Creating a database
[Ruby] Creating code using the concept of classes and instances
Ruby Learning # 32 Building a Quiz
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
Try using gRPC in Ruby
Ruby ① Build a Windows environment
I wrote a C parser (like) using PEG in Ruby
[ruby] Creating a program that responds only to specific conditions
[Programming Encyclopedia] §2 Try using Ruby
Creating Ruby classes and instances
Make a rhombus using Java
Cut out a Ruby string
(Ruby on Rails6) Creating a database and displaying it in a view
Sorting hashes in a Ruby array
Get date without using Calendar class
Ruby Learning # 19 Building a Better Calculator
Create a Jetty project using Eclipse
Create a tomcat project using Eclipse
Calendar creation problem (fun Ruby practice problem)
[Rails] Creating a new project with rails new
Upload a file using Java HttpURLConnection
Install Ruby 2.5 on CentOS 7 using SCL
Creating a timer app with a muddy
[Ruby] It's a convenient guy ~ before_action ~
Ruby Learning # 22 Building a Guessing Game
Create a Java project using Eclipse
Implement a gRPC client in Ruby
Create a filtering function using acts-as-taggable-on
[Complete programming] §3 Let's calculate using Ruby!
Java Calendar is not a singleton.
[Ruby] Search problem using index method
Using a local network on iOS 14
What is a Ruby 2D array?
Make a typing game with ruby
How to create a query using variables in GraphQL [Using Ruby on Rails]