# Re: Runt: Matching the year

**URL:** <https://www.ruby-forum.com/t/re-runt-matching-the-year/50346>\
**Category:** Ruby\
**Created:** [December 15, 2005, 3:41pm UTC](https://www.ruby-forum.com/t/re-runt-matching-the-year/50346 "2005-12-15T15:41:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![LipperS\_Matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8491ac/32.png) [@LipperS\_Matthew](https://www.ruby-forum.com/u/LipperS_Matthew)\
**Post date:** [December 15, 2005, 3:41pm UTC](https://www.ruby-forum.com/t/re-runt-matching-the-year/50346/1 "2005-12-15T15:41:59Z")

</div>

> I downloaded runt to do some temporal expression work, and  
> was wondering if there’s any way to match against a year.  
> From what I can tell I can match against month, day, hour,  
> min, sec, but not the year.

Well, strictly speaking you could use a DateRange constructed  
with January 1st and December 31st of the year(s) in question but this  
is a pain…

> Basically I’d like to have a timex that matches all dates  
> with the year 2005. If I create a date that has the year  
> 2006, then include?  
> should return false for that particular date. Am I missing  
> something in the docs? I’d appreciate any help.
> 
> Pat

I’ve just added the following class to CVS (apologize if Outlook mangles  
the layout)

# Simple expression which returns true if the supplied arguments

# occur within the given year.

# 

class YearTE

def initialize(year)  
@year = year  
end

def include?(date)  
return date.year == @year  
end

end

For an example of usage see the test case in  
test/temporalexpressiontest.rb. Here’s an excerpt:

def test\_year\_te

# second sun of any month

second\_sun = DIMonth.new(Second, Sunday)

# simple year matching expression which will return true for

# any date in 2005

year\_te = YearTE.new(2005)

# Second Sunday of a month in 2002

dt\_in\_2002 = Date.civil(2002,9,8)

# Second Sunday of a month in 2005

dt\_in\_2005 = Date.civil(2005,12,11)

# Matches

assert(year\_te.include?(dt\_in\_2005))

# Does not match

assert(!year\_te.include?(dt\_in\_2002))  
end

Feel free to contact me directly if you have any questions or problems.

Thanks for using Runt!

Matt
