# Could this one line Date extension to return the end of financial year date be any more compact?

**URL:** <https://www.ruby-forum.com/t/could-this-one-line-date-extension-to-return-the-end-of-financial-year-date-be-any-more-compact/156014>\
**Category:** Ruby\
**Created:** [December 19, 2008, 8:35am UTC](https://www.ruby-forum.com/t/could-this-one-line-date-extension-to-return-the-end-of-financial-year-date-be-any-more-compact/156014 "2008-12-19T08:35:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [December 19, 2008, 8:35am UTC](https://www.ruby-forum.com/t/could-this-one-line-date-extension-to-return-the-end-of-financial-year-date-be-any-more-compact/156014/1 "2008-12-19T08:35:25Z")

</div>

could this be any more compact or ruby like that it is?

class Date

def _financial\_year\_end_  
Date.civil(year, 6, 30) \< self ? Date.civil(year, 6, 30) :  
Date.civil(year - 1, 6, 30)  
end

end

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [December 19, 2008, 5:31pm UTC](https://www.ruby-forum.com/t/could-this-one-line-date-extension-to-return-the-end-of-financial-year-date-be-any-more-compact/156014/2 "2008-12-19T17:31:29Z")

</div>

On Dec 19, 2008, at 2:26 AM, Greg H. wrote:

> could this be any more compact or ruby like that it is?
> 
> class Date
> 
> def _financial\_year\_end_  
> Date.civil(year, 6, 30) \< self ? Date.civil(year, 6, 30) :  
> Date.civil(year - 1, 6, 30)  
> end
> 
> end

Well, you could avoid the multiple date creations and step the date  
back 12 months when needed.

class Date  
def financial\_year\_end  
if (this\_year = Date.civil(year, 6, 30)) \< self  
this\_year  
else  
this\_year \<\< 12  
end  
end  
end

irb\> jun15 = Date.civil(2008, 6, 15)  
=\> #\<Date: 4909265/2,0,2299161\>  
irb\> jul15 = Date.civil(2008, 7, 15)  
=\> #\<Date: 4909325/2,0,2299161\>  
irb\> puts jun15  
2008-06-15  
=\> nil  
irb\> puts jul15  
2008-07-15  
=\> nil  
irb\> puts jun15, jun15.financial\_year\_end  
2008-06-15  
2007-06-30  
=\> nil  
irb\> puts jul15, jul15.financial\_year\_end  
2008-07-15  
2008-06-30  
=\> nil

The name seems odd to me as the expectation that _I_ had was to ask a  
date when the financial year that it is part of will end. However,  
it’s your method, so just document what you mean it to be.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [December 19, 2008, 10:06pm UTC](https://www.ruby-forum.com/t/could-this-one-line-date-extension-to-return-the-end-of-financial-year-date-be-any-more-compact/156014/3 "2008-12-19T22:06:37Z")

</div>

thanks - I like the “this\_year \<\< 12” - didn’t realize this was  
available  
for dates

On Sat, Dec 20, 2008 at 2:22 AM, Rob B.
