Search code examples
rubylinuxfile-permissionsruby-1.8.7

Equivalent of chmod +x in Ruby


In Linux, it is very easy "just" to add executable to a file, simply by input:

chmod +x <fname>

However, I have failed to find something as easy in the gems of Ruby. Of course, one could do a system call, i.e.

system( 'chmod +x' << fname )

However, I am looking for something more 'elegant'.

I'm using Ruby 1.8.7.


Solution

  • You can read the current mode using File.stat and then bitwise '''or''' it with a mask to achieve what you want. Here is a sample (which could be shortened):

    current_mask = File.stat('foo.sh').mode new_mask = current_mask | '0000000000000001'.to_i(2) File.chmod(new_mask, 'foo.sh')