user1134991
user1134991

Reputation: 3103

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.

Upvotes: 3

Views: 3317

Answers (2)

deivid
deivid

Reputation: 5268

require "fileutils"

FileUtils.chmod("+x", "foo.sh")

Upvotes: 17

Pascal
Pascal

Reputation: 8656

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

Upvotes: 2

Related Questions