Edit: As Naklion's answer rightly points out 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.
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.
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.
I'd call the first parameter
offset
rather thanday_of_week
. For one,day_of_week
would seem to imply that you can use aDate
object'swday
as the offset, butwday
's range is 0-6, with zero being Sunday, so that won't work. Also, withday_of_week
I wonder if you mean a day's name, or its number - oh? Oh, and which week are we talking about? The nameoffset
is a lot more generic, but that might be a good thing here.
I've also spelled outprint_calendar
andmonth_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 doneputs %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 inArray
(andand those mixed-in by theEnumerable
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'erwe're constructing adates
array ofnil
s that'soffset
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 a number ofzero 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
week
s are just arrays orofnil
s and/or numbers, so we convert each of them, usingmap
, to arraysan array of strings. Each blank/number is first converted to a string, and then padded with spaces usingrjust
. Finally, this mapped array is joined and output.
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.
I'd call the first parameter
offset
rather thanday_of_week
. For one,day_of_week
would seem to imply that you can use aDate
object'swday
as the offset, butwday
's range is 0-6, with zero being Sunday, so that won't work. Also, withday_of_week
I wonder if you mean a day's name, or its number - oh, and which week are we talking about?offset
is a lot more generic, but that might be a good thing here.
I've also spelled outprint_calendar
andmonth_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 doneputs %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 inArray
(and those mixed-in by theEnumerable
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'er constructing adates
array ofnil
s that'soffset
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 a number of "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
week
s are just arrays ornil
s and/or numbers, so we convert them, usingmap
, to arrays of strings. Each blank/number is first converted to a string, and then padded with spaces usingrjust
. Finally, this mapped array is joined and output
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, instead of a text blob.
As for printing, you don't need to change anything, since puts
automatically adds linebreaks when printing an array.
I'd call the first parameter
offset
rather thanday_of_week
. For one,day_of_week
would seem to imply that you can use aDate
object'swday
as the offset, butwday
's range is 0-6, with zero being Sunday, so that won't work. Also, withday_of_week
I wonder if you mean a day's name, or its number? Oh, and which week are we talking about? The nameoffset
is a lot more generic, but that might be a good thing here.
I've also spelled outprint_calendar
andmonth_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 doneputs %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 inArray
and those mixed-in by theEnumerable
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 adates
array ofnil
s that'soffset
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
week
s are just arrays ofnil
s and/or numbers, so we convert each of them, usingmap
, to an array of strings. Each blank/number is first converted to a string, and then padded with spaces usingrjust
. Finally, this mapped array is joined and output.
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.
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, instead of a text blob.
As for printing, you don't need to change anything, since puts
automatically adds linebreaks when printing an array.
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, instead of a text blob.
As for printing, you don't need to change anything, since puts
automatically adds linebreaks when printing an array.