From: Jeremy Bopp Date: 2012-02-07T05:14:59+09:00 Subject: Re: Make a file read-only? On 02/06/2012 01:38 PM, Intransition wrote: > Hi, I can make a file writeable easy enough, > > mode = File.stat(file).mode > File.chmod(mode | 0000200, file) > > But how do I make a file read-only? > > mode = File.stat(file).mode > File.chmod(mode - 0000222, file) > > Subtraction doesn't work. And I've tried `&`, `|` and `^`. Do I have to > do some sort compliments operation here? What's the trick to this? > > FYI, on the command line it's as easy as: > > $ chmod a-w file More specifically, then, you want to remove the write permission from the file, leaving it whatever other permissions were already set (possibly none!). In other words, you want to /mask out/ the write bits from whatever the current file mode is. Try the following: mode = 0100664 mask = mode ^ 0222 new_mode = mode & mask -Jeremy