patternrubyModerate
Calendar Print Method
Viewed 0 times
printmethodcalendar
Problem
I'm currently learning Ruby and need help in editing code to be more idiomatic. I have the method below I could use some pointers on how to make more "Ruby" :-)
def print_cal(day_of_week, month_len)
days = %w(Mon Tue Wed Thu Fri Sat Sun)
days.each { |day| print "#{day} " }
puts
day_num = 1
month_started = false
while day_num i && month_started == false
print ' '
else
month_started = true
print ' ' if day_num < 10
print " #{day_num} " if day_num <= month_len
day_num += 1
end
end
puts
end
endSolution
Here's my take, with explanations below:
-
I'd call the first parameter
I've also spelled out
-
Also note that the offset behaves slightly different from yours: In your method, the minimum offset is 1. That makes sense if you think "start on the 1st day", but it makes less sense if you think "offset everything by 1". So here, the minimum offset is zero.
-
-
In Ruby, you rarely have to use
Here, we're constructing a
In other words,
-
-
Each of these
And that's it, really.
Calling
Mon Tue Wed Thu Fri Sat Sun
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
However, it'd be even more idiomatic to let the method simply return the text without printing it. That could be done like so:
After which you can do something like
to print it.
Edit: As Naklion's answer rightly points out, it'd be even nicer to return an array of strings (the individual lines of text). In this case, you can do so by simply omitting the
As for printing, you don't need to change anything, since
def print_calendar(offset, month_length)
puts "Mon Tue Wed Thu Fri Sat Sun"
dates = [nil] * offset + (1..month_length).to_a
dates.each_slice(7) do |week|
puts week.map { |date| date.to_s.rjust(3) }.join(' ')
end
end-
I'd call the first parameter
offset rather than day_of_week. For one, day_of_week would seem to imply that you can use a Date object's wday as the offset, but wday's range is 0-6, with zero being Sunday, so that won't work. Also, with day_of_week I wonder if you mean a day's name, or its number? Oh, and which week are we talking about? The name offset is a lot more generic, but that might be a good thing here.I've also spelled out
print_calendar and month_length entirely. While those names weren't too confusing, there's no reason to skip a few letters.-
Also note that the offset behaves slightly different from yours: In your method, the minimum offset is 1. That makes sense if you think "start on the 1st day", but it makes less sense if you think "offset everything by 1". So here, the minimum offset is zero.
-
Array#join is a nice method to know when you have to print lists of stuff (in any language, not just Ruby). You could have done puts %w(Mon Tue Wed Thu Fri Sat Sun).join(' ') instead of your first 3 lines. But the end result would be no different than simply printing a hard-coded string, so that's what we're doing here instead.-
In Ruby, you rarely have to use
while loops. Generally, it's easier to construct and manipulate arrays using the built-in methods in Array and those mixed-in by the Enumerable module. (Seriously, if you're learning Ruby, go read as much as you can from those two links; it's some the most useful stuff in Ruby, and you'll be using it a lot.)Here, we're constructing a
dates array of nils that's offset items long by using Ruby's nifty ability to multiply an array by a number. Then we use + to concatenate that array with an array that's just the numbers 1..month_length.In other words,
dates consists of zero or more "blanks" (the offset) followed by the date numbers of the month.-
each_slice should be fairly self-explanatory: Go through an array, X items at a time. In this case we go through it 7 items at a time (i.e. a week), which is what we need for each row/line of text.-
Each of these
weeks are just arrays of nils and/or numbers, so we convert each of them, using map, to an array of strings. Each blank/number is first converted to a string, and then padded with spaces using rjust. Finally, this mapped array is joined and output.And that's it, really.
Calling
print_calendar(2, 31) will print the following:Mon Tue Wed Thu Fri Sat Sun
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
However, it'd be even more idiomatic to let the method simply return the text without printing it. That could be done like so:
def format_calendar(offset, month_length)
lines = [ "Mon Tue Wed Thu Fri Sat Sun" ]
dates = [nil] * offset + (1..month_length).to_a
dates.each_slice(7) do |week|
lines << week.map { |date| date.to_s.rjust(3) }.join(' ')
end
lines.join("\n")
endAfter which you can do something like
puts format_calendar(2, 31)to print it.
Edit: As Naklion's answer rightly points out, it'd be even nicer to return an array of strings (the individual lines of text). In this case, you can do so by simply omitting the
.join("\n") on the last line of the method above. That'll give you more structured data to work with, instead of a text blob.As for printing, you don't need to change anything, since
puts automatically adds linebreaks when printing an array.Code Snippets
def print_calendar(offset, month_length)
puts "Mon Tue Wed Thu Fri Sat Sun"
dates = [nil] * offset + (1..month_length).to_a
dates.each_slice(7) do |week|
puts week.map { |date| date.to_s.rjust(3) }.join(' ')
end
enddef format_calendar(offset, month_length)
lines = [ "Mon Tue Wed Thu Fri Sat Sun" ]
dates = [nil] * offset + (1..month_length).to_a
dates.each_slice(7) do |week|
lines << week.map { |date| date.to_s.rjust(3) }.join(' ')
end
lines.join("\n")
endputs format_calendar(2, 31)Context
StackExchange Code Review Q#64467, answer score: 11
Revisions (0)
No revisions yet.