# Pulling elements into a method

**URL:** <https://www.ruby-forum.com/t/pulling-elements-into-a-method/128347>\
**Category:** Ruby\
**Created:** [February 6, 2008, 11:20pm UTC](https://www.ruby-forum.com/t/pulling-elements-into-a-method/128347 "2008-02-06T23:20:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![grooveska](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@grooveska](https://www.ruby-forum.com/u/grooveska)\
**Post date:** [February 6, 2008, 11:20pm UTC](https://www.ruby-forum.com/t/pulling-elements-into-a-method/128347/1 "2008-02-06T23:20:53Z")

</div>

I am new to ruby and I’m having trouble with a script I’m working on.  
It probably is something simple that I am missing. I am working on a  
script to delete some files from several directories.

Here is the part of the script I’m having problems with:

time = Time.now #set time object to the current time  
month = time.month #grab the number of the month from the  
current  
day = time.day #grab the day of the current time  
year = time.year #grab the year of the current time

day = day - 14  
def delete\_old\_files(dir, time)  
Dir.chdir(dir) do  
Dir.foreach(".") do |entry|  
next if File.stat(entry).directory? #prevent from deleting  
directories

#Use the modification time  
if File.mtime(entry) \< time  
File.unlink(entry)  
end  
end  
end  
end

cleanup\_dirs = [“E:/oracle/MB4/oraarch/”, “E:/oracle/MB3/oraarch/”,  
“E:/oracle/MB5/oraarch”, “F:/oracle/MBA/oraarch/”, “F:/oracle/MB2/  
oraarch/”, “F:/oracle/MB3/oraarch/”, “F:/oracle/MB/oraarch/”]  
cleanup\_dirs.each do |cleanup\_directory|

delete\_old\_files(cleanup\_directory,Time.local(year,month,day,00,00,0))

end

I should probably use a block or proc or something like that. Any  
advice is greatly appreciated. I’m getting the argument out of range  
error.

Thanks.

---

<div class="post-metadata">

**Author:** ![grooveska](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@grooveska](https://www.ruby-forum.com/u/grooveska)\
**Post date:** [February 6, 2008, 11:41pm UTC](https://www.ruby-forum.com/t/pulling-elements-into-a-method/128347/2 "2008-02-06T23:41:30Z")

</div>

grooveska wrote:

> I’m getting the argument out of range  
> error.
> 
> Thanks.

Post the exact error message, and also post which line in your code that  
the error message refers to.

---

<div class="post-metadata">

**Author:** ![grooveska](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@grooveska](https://www.ruby-forum.com/u/grooveska)\
**Post date:** [February 6, 2008, 11:42pm UTC](https://www.ruby-forum.com/t/pulling-elements-into-a-method/128347/3 "2008-02-06T23:42:39Z")

</div>

On 06.02.2008 23:16, grooveska wrote:

> year = time.year #grab the year of the current time  
> File.unlink(entry)  
> delete\_old\_files(cleanup\_directory,Time.local(year,month,day,00,00,0))
> 
> end
> 
> I should probably use a block or proc or something like that. Any  
> advice is greatly appreciated. I’m getting the argument out of range  
> error.

At a shell prompt on a proper operating system you can do something like  
this:

find your\_dir -type f -mtime +14 -print0 | xargs -r0 rm

If you want to do it in Ruby you can do

require ‘find’

limit = Time.now - 60_60_24\*14

cleanup\_dirs.each do |dir|  
Find.find dir do |f|  
File.unlink f if File.file? f &&  
File.mtime(f) \< limit  
end  
end

Cheers

robert

---

<div class="post-metadata">

**Author:** ![grooveska](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@grooveska](https://www.ruby-forum.com/u/grooveska)\
**Post date:** [February 6, 2008, 11:50pm UTC](https://www.ruby-forum.com/t/pulling-elements-into-a-method/128347/4 "2008-02-06T23:50:36Z")

</div>

grooveska wrote:

> Here is the part of the script I’m having problems with:
> 
> time = Time.now #set time object to the current time  
> month = time.month #grab the number of the month from the  
> current  
> day = time.day #grab the day of the current time  
> year = time.year #grab the year of the current time
> 
> day = day - 14  
> def delete\_old\_files(dir, time)  
> Dir.chdir(dir) do  
> Dir.foreach(".") do |entry|  
> next if File.stat(entry).directory? #prevent from deleting  
> directories
> 
> #Use the modification time  
> if File.mtime(entry) \< time  
> File.unlink(entry)  
> end  
> end  
> end  
> end
> 
> cleanup\_dirs = [“E:/oracle/MB4/oraarch/”, “E:/oracle/MB3/oraarch/”,  
> “E:/oracle/MB5/oraarch”, “F:/oracle/MBA/oraarch/”, “F:/oracle/MB2/  
> oraarch/”, “F:/oracle/MB3/oraarch/”, “F:/oracle/MB/oraarch/”]  
> cleanup\_dirs.each do |cleanup\_directory|
> 
> delete\_old\_files(cleanup\_directory,Time.local(year,month,day,00,00,0))
> 
> end
> 
> I should probably use a block or proc or something like that. Any  
> advice is greatly appreciated. I’m getting the argument out of range  
> error.
> 
> Thanks.

Pay attention to this code:

day = day - 14  
…  
…  
Time.local(year,month,day,00,00,0)

---

<div class="post-metadata">

**Author:** ![grooveska](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@grooveska](https://www.ruby-forum.com/u/grooveska)\
**Post date:** [February 6, 2008, 11:53pm UTC](https://www.ruby-forum.com/t/pulling-elements-into-a-method/128347/5 "2008-02-06T23:53:13Z")

</div>

On Feb 6, 2008 4:19 PM, grooveska [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> year = time.year #grab the year of the current time  
> File.unlink(entry)  
> delete\_old\_files(cleanup\_directory,Time.local(year,month,day,00,00,0))
> 
> end
> 
> I should probably use a block or proc or something like that. Any  
> advice is greatly appreciated. I’m getting the argument out of range

Well, for one thing, your day will be negative if it is less than 14.  
I would think you’d be better off using Date instead of Time. To  
illustrate (for today)…

irb:(main):001:0\> require ‘date’  
=\> true  
irb:(main):002:0\> d = Date.today  
=\> #\<Date: 4908977/2,0,2299161\>  
irb:(main):003:0\> d.day - 14  
=\> -8  
irb:(main):004:0\> d.to\_s  
=\> “2008-02-06”  
irb:(main):005:0\> (d - 14).to\_s  
=\> “2008-01-23”  
irb:(main):006:0\> Time.local(2008, 2, -8)  
Argument Error: argument out of range…

hth,  
Todd
