# Non exclusive file access

**URL:** <https://www.ruby-forum.com/t/non-exclusive-file-access/92422>\
**Category:** Ruby\
**Created:** [April 19, 2007, 12:08pm UTC](https://www.ruby-forum.com/t/non-exclusive-file-access/92422 "2007-04-19T12:08:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bino\_oetomo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5f9b8f/32.png) [@bino\_oetomo](https://www.ruby-forum.com/u/bino_oetomo)\
**Post date:** [April 19, 2007, 12:08pm UTC](https://www.ruby-forum.com/t/non-exclusive-file-access/92422/1 "2007-04-19T12:08:50Z")

</div>

Dear All.

I’m trying to play with LCD4Linux  
There is fascility from LCD4Linux that let it to read to a file, and  
refresh  
the LCD with the content of file being read.

For a dirty trial , I use a Curse driver for LCD4Linux display.  
It emulate 4 line LCD  
I made 4 file , each intended for each line of the LCD

Next , I wrote simple BASH script below :

—START----  
#!/bin/sh  
a=0  
b=0  
while true  
do  
a=$(($a+1))  
b=$(($b+1))  
f="/mnt/ramdisk0/line"$a  
echo $b \>$f  
if [“$a” -eq 4]  
then  
a=0  
fi  
done

----STOP----

Let the script run.  
While the script run … I open 2nd console  
And from that last console I run lcd4linux … and find that it work  
just  
like what I want.  
LCD4Linux read the file … and display the content perfectly.

Next …  
I tried to do it with ruby :

—Start----  
irb  
irb(main):001:0\> my\_file = File.new("/mnt/ramdisk0/line3",  
modestring=“a+”)  
=\> #\<File:/mnt/ramdisk0/line3\>  
irb(main):002:0\> my\_file.flock(File::LOCK\_UN)  
=\> 0  
irb(main):003:0\>  
irb(main):004:0\* my\_file.puts “write it”  
=\> nil  
----Stop—

The LCD4Linux can not read the file … so it can not display the content  
to  
LCD.

But, when I try :

irb(main):005:0\> exit

And back to my main Shell … LCD4Linux can read the file and display  
the  
content.

2 Question :

1. How to make rubby to do shared file access ?
2. the file.puts command always write to the end of the file, How to  
make  
ruby just overide the content of the file with it’s output ?  
(just like bash ==\> echo “something” \>./thisfile )

Sincerely  
-bino-

---

<div class="post-metadata">

**Author:** ![bino\_oetomo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5f9b8f/32.png) [@bino\_oetomo](https://www.ruby-forum.com/u/bino_oetomo)\
**Post date:** [April 19, 2007, 12:51pm UTC](https://www.ruby-forum.com/t/non-exclusive-file-access/92422/2 "2007-04-19T12:51:02Z")

</div>

bino\_oetomo wrote:

> —Start----  
> irb  
> irb(main):001:0\> my\_file = File.new(“/mnt/ramdisk0/line3”, modestring=“a+”)  
> my\_file = File.open(“/mnt/ramdisk0/line3”, “w”)

> 1. the file.puts command always write to the end of the file, How to make  
> ruby just overide the content of the file with it’s output ?  
> (just like bash ==\> echo “something” \>./thisfile )

The mode “a+” appends to a file, “w” overwrites an existing file, or  
creates a new file.  
See here:

> **[Ruby | zenspider.com | by ryan davis](https://www.zenspider.com/ruby/)**
>
> Ruby facts, opinions, source code, links, and stuff.

–  
Phillip “CynicalRyan” Gawlowski  
[http://cynicalryan.110mb.com/](http://cynicalryan.110mb.com/)  
[http://clothred.rubyforge.org](http://clothred.rubyforge.org)

Rule of Open-Source Programming #13:

Your first release can always be improved upon.
