# Filelocking

**URL:** <https://www.ruby-forum.com/t/filelocking/165098>\
**Category:** Ruby\
**Created:** [April 9, 2009, 3:07am UTC](https://www.ruby-forum.com/t/filelocking/165098 "2009-04-09T03:07:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bloodniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bloodniece](https://www.ruby-forum.com/u/bloodniece)\
**Post date:** [April 9, 2009, 3:07am UTC](https://www.ruby-forum.com/t/filelocking/165098/1 "2009-04-09T03:07:57Z")

</div>

Hi All,

I have posted the same code b4, but could not find my original post,  
however this is a different question. I am a newb, so here I go.  
Thank you!

This is my error:

$ ruby test.rb  
test.rb:46: undefined method `flock' for main:Object (NoMethodError) from test.rb:45:in`open’  
from test.rb:45  
from test.rb:42:in `each’  
from test.rb:42

Here is my code:

#!/usr/bin/ruby -w

require ‘ftools’  
array1 = Dir.glob("\*.log")  
if ( array1.length \>= 1 )  
array1.each { |file|  
File.copy(file, “#{file}.orig”)  
if (File.size?(file) \> FSIZE)  
File.open(file, ‘r+’) do |f|  
flock(f, File::LOCK\_SH) do |f|  
f \<\< “i got a lock”  
end  
end  
end  
}  
else  
raise StandardError, “Array ‘array1’ has no values, logs will not be  
rolled!”  
end

---

<div class="post-metadata">

**Author:** ![bloodniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bloodniece](https://www.ruby-forum.com/u/bloodniece)\
**Post date:** [April 9, 2009, 3:15am UTC](https://www.ruby-forum.com/t/filelocking/165098/2 "2009-04-09T03:15:47Z")

</div>

Derek S. wrote:

> test.rb:46: undefined method `flock’ for main:Object (NoMethodError)  
> …  
> File.open(file, ‘r+’) do |f|  
> flock(f, File::LOCK\_SH) do |f|

That should be File.flock.

---

<div class="post-metadata">

**Author:** ![bloodniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bloodniece](https://www.ruby-forum.com/u/bloodniece)\
**Post date:** [April 9, 2009, 3:27am UTC](https://www.ruby-forum.com/t/filelocking/165098/3 "2009-04-09T03:27:08Z")

</div>

Joel VanderWerf wrote:

> Derek S. wrote:
> 
> > test.rb:46: undefined method `flock’ for main:Object (NoMethodError)  
> > …  
> > File.open(file, ‘r+’) do |f|  
> > flock(f, File::LOCK\_SH) do |f|
> 
> That should be File.flock.

OK tried that…thx!, but now I am seeing

**STDERR**

test.rb:46: undefined method `flock' for File:Class (NoMethodError) from test.rb:45:in`open’  
from test.rb:45  
from test.rb:42:in `each’  
from test.rb:42

**CODE**

if ( array1.length \>= 1 )  
array1.each { |file|  
File.copy(file, “#{file}.orig”)  
if (File.size?(file) \> FSIZE)  
File.open(file, ‘r+’) do |f|  
File.flock(f, File::LOCK\_SH) do |f|  
f \<\< “i got a lock”  
end  
end  
end  
}  
else  
raise StandardError, “Array ‘array1’ has no values, logs will not be  
rolled!”  
end

---

<div class="post-metadata">

**Author:** ![bloodniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bloodniece](https://www.ruby-forum.com/u/bloodniece)\
**Post date:** [April 9, 2009, 3:36am UTC](https://www.ruby-forum.com/t/filelocking/165098/4 "2009-04-09T03:36:55Z")

</div>

Derek S. wrote:

> Joel VanderWerf wrote:
> 
> > Derek S. wrote:
> > 
> > > test.rb:46: undefined method `flock’ for main:Object (NoMethodError)  
> > > …  
> > > File.open(file, ‘r+’) do |f|  
> > > flock(f, File::LOCK\_SH) do |f|  
> > > That should be File.flock.

sorry, my fault… should be

f.flock(File::LOCK\_SH) do  
…  
end

Take a look at `ri flock` for details.

---

<div class="post-metadata">

**Author:** ![bloodniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bloodniece](https://www.ruby-forum.com/u/bloodniece)\
**Post date:** [April 9, 2009, 3:40am UTC](https://www.ruby-forum.com/t/filelocking/165098/5 "2009-04-09T03:40:25Z")

</div>

> ```
> from test.rb:45
> from test.rb:42:in `each'
> from test.rb:42
> 
> ```

File#flock =\> f.flock

# ======================================================================= This email, including any attachments, is only for the intended addressee. It is subject to copyright, is confidential and may be the subject of legal or other privilege, none of which is waived or lost by reason of this transmission. If the receiver is not the intended addressee, please accept our apologies, notify us by return, delete all copies and perform no other act on the email. Unfortunately, we cannot warrant that the email has not been altered or corrupted during transmission.
