mirror of
https://github.com/jbranchaud/til
synced 2026-01-05 16:18:01 +00:00
Add Shift The Month On A Date Object as a Ruby TIL
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||
|
||||
_1324 TILs and counting..._
|
||||
_1325 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -1116,6 +1116,7 @@ _1324 TILs and counting..._
|
||||
- [Scripting With RVM](ruby/scripting-with-rvm.md)
|
||||
- [Scroll To Top Of Page With Capybara](ruby/scroll-to-top-of-page-with-capybara.md)
|
||||
- [Set RVM Default Ruby](ruby/set-rvm-default-ruby.md)
|
||||
- [Shift The Month On A Date Object](ruby/shift-the-month-on-a-date-object.md)
|
||||
- [Show Public Methods With Pry](ruby/show-public-methods-with-pry.md)
|
||||
- [Silence The Output Of A Ruby Statement In Pry](ruby/silence-the-output-of-a-ruby-statement-in-pry.md)
|
||||
- [Single And Double Quoted String Notation](ruby/single-and-double-quoted-string-notation.md)
|
||||
|
||||
40
ruby/shift-the-month-on-a-date-object.md
Normal file
40
ruby/shift-the-month-on-a-date-object.md
Normal file
@@ -0,0 +1,40 @@
|
||||
# Shift The Month On A Date Object
|
||||
|
||||
One of things that Ruby loves to do is overload operators to support
|
||||
specialized class-specific functionality. For instance, with the `Date` class,
|
||||
you can use the `+` and `-` operators to add or remove days from a given
|
||||
`Date`.
|
||||
|
||||
```ruby
|
||||
> Date.today
|
||||
=> #<Date: 2023-07-13 ((2460139j,0s,0n),+0s,2299161j)>
|
||||
> Date.today + 1
|
||||
=> #<Date: 2023-07-14 ((2460140j,0s,0n),+0s,2299161j)>
|
||||
> Date.today - 3
|
||||
=> #<Date: 2023-07-10 ((2460136j,0s,0n),+0s,2299161j)>
|
||||
```
|
||||
|
||||
That one feels pretty natural to me.
|
||||
|
||||
The `Date` class overloads another operator to do something that doesn't feel
|
||||
quite as natural.
|
||||
|
||||
The `<<` operator will shift (increment or decrement) the month of the given
|
||||
`Date` object. Given a positive number, it will shift the date that many months
|
||||
in the future (even wrapping to a new year as necessary). Given a negative
|
||||
number, it will shift the date back in time that many months.
|
||||
|
||||
```ruby
|
||||
> Date.today
|
||||
=> #<Date: 2023-07-13 ((2460139j,0s,0n),+0s,2299161j)>
|
||||
> Date.today << 1
|
||||
=> #<Date: 2023-06-13 ((2460109j,0s,0n),+0s,2299161j)>
|
||||
> Date.today << -2
|
||||
=> #<Date: 2023-09-13 ((2460201j,0s,0n),+0s,2299161j)>
|
||||
> Date.today << 6
|
||||
=> #<Date: 2023-01-13 ((2459958j,0s,0n),+0s,2299161j)>
|
||||
```
|
||||
|
||||
This is a bit clever for my liking, but fun to know about.
|
||||
|
||||
[source](https://ruby-doc.org/stdlib-3.0.0/libdoc/date/rdoc/Date.html#method-i-3C-3C)
|
||||
Reference in New Issue
Block a user