Ruby中擴展File


class File
def self.write filename, content
File.open(filename, 'wb') do |file|
file.write content
end
end

def self.append filename, content
File.open(filename, 'ab') do |file|
file.write content
end
end

def self.expand_path_restricted(file_name, dir_string, restrict_dir_string = nil)
dir_string = expand_path(dir_string)
restrict_dir_string = restrict_dir_string ? expand_path(restrict_dir_string) : dir_string

expanded = expand_path(file_name, dir_string)

unless expanded.index(restrict_dir_string) == 0
raise "Restricted file path #{file_name.inspect} must be inside #{restrict_dir_string.inspect}"
end

expanded
end
end
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章