class Ld::File

Attributes

exist[RW]
mode[RW]
name[RW]
path[RW]
size[RW]
stat[RW]
suffix[RW]
type[RW]

Public Class Methods

base64(path) click to toggle source

编码base64

# File lib/ld/file/file.rb, line 21
def self.base64 path
  Base64.encode64(File.open(path, 'rb').read)
end
current() click to toggle source

作用 返回当前所在目录(Dir.pwd)

# File lib/ld/file/file.rb, line 60
def self.current
  Ld::File.new @@current_path
end
new(path) click to toggle source
# File lib/ld/file/file.rb, line 7
def initialize path
  @path = path[0] == '/' ? path : "#{Dir.pwd}/#{path}"
  @name = File.basename @path
  read_attrs
end
open(path) click to toggle source

作用 打开一个文件

# File lib/ld/file/file.rb, line 14
def self.open path
  f = self.new path
  f.read_attrs
  f
end
test() click to toggle source

test:

# File lib/ld/file/file.rb, line 206
def self.test
  sdf{100.times{Ld::File.new('app').search_regexp //}}
end
write(path, arr) click to toggle source
# File lib/ld/file/file.rb, line 201
def self.write path, arr
  File.open(path)
end
write_image_by_base64_and_path(base64, path) click to toggle source

解码base64,并写入图片

# File lib/ld/file/file.rb, line 26
def self.write_image_by_base64_and_path base64, path
  File.open(path,'wb').write(Base64.decode64 base64)
end

Public Instance Methods

children() click to toggle source

作用 返回这个目录下的所有一级目录与一级文件,如果不是目录,会报错

# File lib/ld/file/file.rb, line 50
def children
  dir!
  Dir.foreach(@path).map{|n| Ld::File.new("#{@path}/#{n}") if !is_exclude? n}.compact.sort{|a,b| a.type <=> b.type}
end
delete() click to toggle source

作用 删除当前文件(有gets确认)

# File lib/ld/file/file.rb, line 162
def delete
  puts "删除!:#{path}\n,确认请输入 delete_file,"
  if gets.chomp == 'delete_file'
    if File.delete path == 1
      @exist = false
      puts "删除成功 #{path}"
    end
  end
end
details() click to toggle source
# File lib/ld/file/file.rb, line 217
def details
  Ld::Dir.new(@path).details
end
dir!() click to toggle source
# File lib/ld/file/file.rb, line 78
def dir!
  raise "这不是一个目录,而是一个#{type}:#{path}" if type != 'directory'
end
dir?() click to toggle source

作用 判断这是目录吗

# File lib/ld/file/file.rb, line 69
def dir?
  type == 'directory'
end
dirs() click to toggle source

作用 返回所有目录

# File lib/ld/file/file.rb, line 188
def dirs
  children.select{|f| f.type == 'directory'}
end
exist!() click to toggle source
# File lib/ld/file/file.rb, line 64
def exist!
  raise "不存在的 #{path}" if !@exist
end
file!() click to toggle source
# File lib/ld/file/file.rb, line 82
def file!
  raise "这不是一个文件,而是一个#{type}:#{path}" if type != 'file'
end
file?() click to toggle source

作用 判断这是文件吗

# File lib/ld/file/file.rb, line 74
def file?
  type == 'file'
end
files() click to toggle source

作用 返回所有文件

# File lib/ld/file/file.rb, line 173
def files
  children.select{|f| f.type == 'file'}
end
find(name) click to toggle source

作用 查找文件或目录,返回一个一级目录或文件,如果不存在则返回nil

# File lib/ld/file/file.rb, line 87
def find name
  dir!
  Ld::File.new "#{path}/#{name.to_s}" if File.exist? "#{path}/#{name.to_s}"
end
is_exclude?(name) click to toggle source
# File lib/ld/file/file.rb, line 55
def is_exclude? name
  @@exclude.include? name
end
iter_search_regexp(regexp, results) click to toggle source
# File lib/ld/file/file.rb, line 133
def iter_search_regexp regexp, results
  children.each do |file|
    if file.name.match(regexp)
      results << file
    end
    if file.dir?
      file.iter_search_regexp regexp, results
    end
  end
end
lines() click to toggle source

作用 如果是一个文本文件,返回所有行

# File lib/ld/file/file.rb, line 145
def lines
  File.open(@path).readlines
end
look() click to toggle source
# File lib/ld/file/file.rb, line 45
def look
  puts lines
end
ls() click to toggle source

作用 输出目录中所有条目

# File lib/ld/file/file.rb, line 193
def ls
  if type == 'directory'
    Ld::Print.ls self
  elsif type == 'file'
    Ld::Print.ls self.parent
  end
end
parent() click to toggle source

作用 返回父目录

# File lib/ld/file/file.rb, line 178
def parent
  Ld::File.new(File.dirname @path)
end
read_attrs() click to toggle source
# File lib/ld/file/file.rb, line 30
def read_attrs
  @suffix = @name.split('.').last
  @suffix = @suffix == @name ? nil : @suffix
  if File.exist? @path
    @exist = true
    @type = File.ftype path
    @stat = File.stat path
    @size = @stat.size
    @mode = @stat.mode
  else
    @exist = false
    @type = 'not found'
  end
end
rename(new_name) click to toggle source

作用 修改名称(目录或文件均可)

# File lib/ld/file/file.rb, line 154
def rename new_name
  new_path = "#{dir.path}/#{new_name}"
  if File.rename @path, new_path
    @path = new_path
  end
end
sdf(&block) click to toggle source
# File lib/ld/file/file.rb, line 210
def sdf &block
  t1 = Time.new
  block.call
  t2 = Time.new
  puts t2 - t1
end
search_regexp(regexp, type = :all) click to toggle source

作用 模糊查找,返回所有匹配的目录和文件

# File lib/ld/file/file.rb, line 108
def search_regexp regexp, type = :all
  dir!
  results = []
  iter_search_regexp regexp, results
  case type.to_s
    when 'all'
      results
    when 'file'
      results.map{|f| f.type == 'file'}
    when 'dir'
      results.map{|f| f.type == 'directory'}
  end
end
siblings() click to toggle source

作用 返回所有兄弟

# File lib/ld/file/file.rb, line 183
def siblings
  parent.children
end