Forum: Ruby Non exclusive file access

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Df13d29fbc02d6979b4e6d3a3ccb4e4a?d=identicon&s=25 bino_oetomo (Guest)
on 2007-04-19 12:08
(Received via mailing list)
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-
Ac0085dae0703db56ad7f8cb9e1798ba?d=identicon&s=25 Phillip Gawlowski (Guest)
on 2007-04-19 12:51
(Received via mailing list)
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")

> 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 )

The mode "a+" appends to a file, "w" overwrites an existing file, or
creates a new file.
See here:
http://www.zenspider.com/Languages/Ruby/QuickRef.html#14


--
Phillip "CynicalRyan" Gawlowski
http://cynicalryan.110mb.com/
http://clothred.rubyforge.org

Rule of Open-Source Programming #13:

Your first release can always be improved upon.
This topic is locked and can not be replied to.