Class: File

Inherits:
Object show all
Defined in:
lib/roebe/core/file.rb

Overview

#

The official ruby documentation of File can be found here:

https://ruby-doc.org/core/File.html

Note that I personally rarely duck-patch any of the core classes and core modules of ruby these days, so the code here is mostly for historic purposes really - I don’t think much new code will be added to these files.

#

Class Method Summary collapse

Class Method Details

.binread(fname) ⇒ Object



25
26
27
28
29
# File 'lib/roebe/setup/setup.rb', line 25

def File.binread(fname)
  open(fname, 'rb') {|f|
    return f.read
  }
end

.delete_if_exists(f, be_verbose = true) ⇒ Object

#

File.delete_if_exists

Use this method to delete a file it if exists. Needs either an absolute path given, or you may have to be in the current, proper, working directory.

Usage example:

File.delete_if_exists 'foo'
File.delete_if_exists '/your/file'
File.delete_if_exists '/your/file' {'blueonblack'} <-- not implemented yet
#


133
134
135
136
137
138
# File 'lib/roebe/core/file.rb', line 133

def self.delete_if_exists( f, be_verbose = true)
  if File.exists? f.to_s
    puts ' => Deleting ' + f.to_s if be_verbose
    File.delete(f.to_s)
  end
end

.delete_if_exists_unless_it_is_a_directory(i) ⇒ Object

#

File.delete_if_exists_unless_it_is_a_directory

Used to delete a file it if exists unless it is a directory.

Usage example:

File.delete_if_exists_unless_it_is_a_directory "/your/file"
#


60
61
62
63
64
65
66
67
68
69
# File 'lib/roebe/core/file.rb', line 60

def self.delete_if_exists_unless_it_is_a_directory(i)
  if File.exist? i
    if File.directory? i
      puts "=> #{i} is a directory, I will not delete it for now."
    else
      # Wrap it to the above method.
      File.delete_if_exists(i)
    end
  end
end

.dir?(path) ⇒ Boolean

for corrupted Windows’ stat(2)

Returns:

  • (Boolean)


32
33
34
# File 'lib/roebe/setup/setup.rb', line 32

def File.dir?(path)
  File.directory?((path[-1,1] == '/') ? path : path + '/')
end
#
#


74
75
76
77
78
79
80
81
82
83
84
# File 'lib/roebe/core/file.rb', line 74

def self.do_symlink(which_existing_file, symlink_name)
  begin
    symlink(which_existing_file, symlink_name)
  rescue => e
    puts 'Small error in class File. Should not be '\
         'very important but error still is displayed '\
         'nevertheless (normally it means the file '\
         'exists already):'
    pp e
  end
end

.is_audio_file?(f) ⇒ Boolean

#

File.is_audio_file?

Used to determine if a file is an audio file or not. ARRAY_AUDIO_FILE_TYPES is in std_constants.rb

Usage:

File.is_audio_file? "/Depot/Audio/WamdueProject_KingOfMyCastle.mp3" # # => true
#

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/roebe/core/file.rb', line 27

def self.is_audio_file?(f)
  file_type = File.extname(f)
  ARRAY_AUDIO_FILE_TYPES.include?(file_type) ? true : false
end

.is_image?(i) ⇒ Boolean

#

is_image?

Used to determine if a file is an image file or not.

Usage Examples: pwdimg

File.is_image? 'SONNENUNTERGANG.jpg'
#

Returns:

  • (Boolean)


114
115
116
117
# File 'lib/roebe/core/file.rb', line 114

def self.is_image?(i)
  file_type = File.extname(i)
  ARRAY_IMAGE_FILE_TYPES.include?(file_type) ? true : false
end

.ownership(file) ⇒ Object

#

File.ownership

This method will return an Array with the ownership information.

First entry will be pwu-id, second will be group-id.

Usage example:

File.ownership '/home/x/data/PROGRAMMING_LANGUAGES/RUBY/src/roebe/lib/roebe/core/file.rb'
#


98
99
100
101
102
# File 'lib/roebe/core/file.rb', line 98

def self.ownership(file)
  require 'etc'
  s = File.stat(file)
  [Etc.getpwuid(s.uid).name, Etc.getgrgid(s.gid).name]
end
#

Anzuwenden wenn das File noch nit bereits existiert.

Usage example:

File.symlink_unless_exists(which_existing_file, name)
#


42
43
44
45
46
47
48
# File 'lib/roebe/core/file.rb', line 42

def self.symlink_unless_exists(which_existing_file, name)
  unless File.exists? name
    symlink(which_existing_file, name)
  else
    puts "File #{name} already exists."
  end
end