[#1649] Re: New Ruby projects — Yukihiro Matsumoto <matz@...>
The following message is a courtesy copy of an article
[#1672] Re: Ruby 1.4 stable manual bug? — Yukihiro Matsumoto <matz@...>
The following message is a courtesy copy of an article
[#1673] Re: Possible problem with ext/socket in 1.5.2 — itojun@...
[#1694] Conventions for our Ruby book — Dave Thomas <Dave@...>
[#1715] Install postgresql support — Ikhlasul Amal <amal@...>
Hi all,
Hi,
[#1786] Is this a bug? — Clemens Hintze <clemens.hintze@...>
(mailed & posted)
[#1814] Objects nested sometimes. — Hugh Sasse Staff Elec Eng <hgs@...>
I am attemptiong to write a package which consists of a workspace
[#1816] Ruby 1.5.3 under Tru64 (Alpha)? — Clemens Hintze <clemens.hintze@...>
Hi all,
Hi,
Yukihiro Matsumoto writes:
Hi,
Hi,
[#1834] enum examples? — Hugh Sasse Staff Elec Eng <hgs@...>
Has anyone any examplse of using the Enumerable module? I've had a
[#1844] Minor irritation, can't figure out how to patch it though! — Hugh Sasse Staff Elec Eng <hgs@...>
I was considering how difficult it would be to patch Ruby to accept
[#1889] [ruby-1.5.3] require / SAFE — ts <decoux@...>
[#1896] Ruby Syntax similar to other languages? — "David Douthitt" <DDouthitt@...>
From: Yukihiro Matsumoto <matz@netlab.co.jp>
[#1900] Enumerations and all that. — Hugh Sasse Staff Elec Eng <hgs@...>
Thank you to the people who responded to my questions about Enumerated
Hugh Sasse Staff Elec Eng <hgs@dmu.ac.uk> writes:
On 16 Mar 2000, Dave Thomas wrote:
[#1929] Re: Class Variables — "David Douthitt" <DDouthitt@...>
| "David Douthitt" <DDouthitt@cuna.com> writes:
[#1942] no Fixnum#new ? — Quinn Dunkan <quinn@...>
Ok, I can add methods to a built-in class well enough (yes I know about succ,
[#1989] English Ruby/Gtk Tutorial? — schneik@...
Hi,
[#2022] rb_global_entry — ts <decoux@...>
[#2036] Anonymous and Singleton Classes — B_DAVISON <Bob.Davison@...>
I am a Ruby newbie and having some problems getting my mind around certain
[#2069] Ruby/GTK+ question about imlib --> gdk-pixbug — schneik@...
[#2073] Re: eval.rb fails — "Dat Nguyen" <thucdat@...>
The doc is fine, this happens only if you try to execute 'until' block
On Wed, 22 Mar 2000, Dat Nguyen wrote:
[#2084] Scope violated by import via 'require'? — Clemens Hintze <c.hintze@...>
Hi,
[#2104] ARGF or $< — Hugh Sasse Staff Elec Eng <hgs@...>
Has anyone any examples of how to use ARGF or $< as I cannot find much
Hi.
[#2165] Ruby strict mode and stand-alone executables. — "Conrad Schneiker" <schneiker@...>
Some people want Ruby to have a strict compile mode.
[#2203] Re: parse bug in 1.5 — schneik@...
[#2212] Re: Ruby/Glade usage questions. — ts <decoux@...>
>>>>> "m" == mrilu <mrilu@ale.cx> writes:
[#2241] setter() for local variables — ts <decoux@...>
[#2256] Multiple assignment of pattern match results. — schneik@...
[#2267] Re: Ruby and Eiffel — h.fulton@...
[#2309] Question about attribute writers — Dave Thomas <Dave@...>
Clemens Hintze <c.hintze@gmx.net> writes:
[ruby-talk:02060] Re: Lockfiles
David Douthitt writes:
| > Here's my code:
| >
| > #!/opt/ruby/bin/ruby
| >
| > class Lock
[...]
| > def setlock (lockf = @lockfile)
| > raise "Lockfile not set!" if lockf == nil;
| > raise "Lock failed!" if
| > ! system("set -o noclobber ; cat /dev/null 2> /dev/null > #{lockf}")
|
| Why do you use such complicate system call here? I would prefer a Ruby
| solution:
| if not File.exist?(lockf)
| begin File.open(lockf, "w") { |fp| fp.write(Process.pid) }
| rescue
| raise "Lock failed (system error)"
| end
| else
| raise "Lock failed (already locked)"
| end
This is not guaranteed to work. This shows the danger of locks: in the
time between the test (File.exist?) and the creation (File.open) someone
else could interfere, and a lock could already be there when File.open
is reached.
The system call goes like this:
set -o noclobber # Don't clobber files when redirected
cat /dev/null 2> /dev/null > lockfile # create lock file, or fail if exists
No chance (I think) of someone getting the lock after we "test" it - since
the test is presumably at a system level.
| > at_exit { self.unlock }
| > @locked_up = true
| > end
[...]
| > def unlock
| > test(?e, @lockfile) if
| > raise "Unlock failed!" if
| > ! system("rm -f #{@lockfile}");
|
| What you are try to doing here? If 'rm -f' failed the exception is
| raised and 'test(?e,...)' will not performed. If 'rm' went well,
| neither 'raise' nor 'test' will be executed.
|
| raise "Unlock failed (no lock)" unless File.exist?(@lockfile)
| begin
| File.delete @lockfile
| rescue
| raise "Unlock failed (couldn't remove lockfile)"
| end
I see I mangled my code :-) What I want was probably more like this
(using your improvements nonetheless):
return if ! File.exist?(@lockfile)
begin
File.delete @lockfile
rescue
raise "Unlock failed! (couldn't remove lockfile)"
end
| Your code is ok! Only there are some things I would like to
| remark.
|
| 1. You often use constants like "/var/spool/locks" throughout your
| whole code.
Definitely! "/var/spool/locks" is meaningless. @lockdir has much
more meaning. It also allows you to change the directory easily.
lockdir = "/var/spool/locks"
could be changed to
lockdir = "/var/run"
or to
lockdir = $*[0]
and so on....
| 2. You use too much system dependend things like system(...)
| here. This it not really necessary.
When you gotta go, you gotta go :-)
| 3. You should not use things like 'test(?e, ...)'. The Ruby classes
| are more descriptive.
I'm still learning....
| 4. IMHO, your code is not carefully designed for the class/instance
| clash. Means, you do some things both in class methods and in
| instance methods.
I'm still finding out what I can do and can't. Tell me more about
this class/instance distinction. I'm forever finding myself wanting
"a bunch of X" where X inevitably turns out to be an instance
and then the first code becomes
Xes = Hash.new
Is this typical?
| 5. You should not on any case define accessors for your class. I
| would consider it not-so-good style if I would have to:
|
| lock = Lock.new("/var/spool/mail/cle")
| lock.lockdir = "/var/spool/mail"
| lock.setlock
|
| following would be better, IMO:
|
| lock = Lock.new("/var/spool/mail/cle", "/var/spool/mail")
| lock.setlock
Ahhhhh..... I was wondering some about that. I have accessors
almost everywhere :-(
| All in all I would say, that your code may be appropiate for what you
| have in mind for your apps. But it is not generic enough, IMHO! No
| steal-the-lock feature. No wait for the lock. No auto-unlock as soon
| as I do not need this file instance anymore...
Steal-the-lock? HORRORS!
Wait for the lock? Interesting....
Auto-unlock? Already there. Check for the "at_exit" call, as well as the
locked method. locked is nice:
mylock.locked {
...do something while locked...
}
....now unlocked....
One think I like about my Lock is that the names are similar to those
of the Thread module - so one less thing to have to learn.