Reputation: 2174
I have an array of dates and I need to ensure that the year within these dates are all sooner than 1900.
My array looks like
[
[0] "1980-10-03",
[1] "1981-10-03",
[2] "2001-10-03"
]
Now what I have been able to do and this works well for me is taking one of the index's and splitting them up to do what I need. Like so
splitted_birth_date = array.first.split("-")
Which gives me an output of
[
[0] "1980",
[1] "10",
[2] "03"
]
In which i've gone on to
splitted_birth_date.first.to_i > 1900?
and so forth been able to make the edits and things that I need for making this task work out.
My problem though, is that I am having trouble looping through all of the elements within my array and splitting them.
I've tried
array.each do |birth_date|
birth_date.to_i
end
But nothing seems to happen. Ultimately I'm having trouble looping through records in an array, and manipulating the necessary parts in them.
Upvotes: 0
Views: 69
Reputation: 110725
From the example I assume that a (possibly empty) array of dates that are before 1900 (and therefore do not meet the requirement) is to be returned. The easiest way to do that is to simply convert the date strings to integers and select the dates for which the integer is less than 1900. For example, "1980-10-03".to_i #=> 1980
.
arr = ["1980-10-03", "1981-10-03", "1899-11-12", "2001-10-03"]
arr.select { |s| s.to_i < 1900 }
#=> ["1899-11-12"]
See String#to_i.
Upvotes: 0
Reputation: 1464
array = ["1980-10-03", "1981-10-03", "2001-10-03"]
yr = 1990
res = array.select{|d| Date.parse(d).year < yr}
Upvotes: 3