File Flashcards
Ruby File
converts relative pathname to an absolute pathname; relative paths are referenced from the current working directory of the process unless dir_string is given
File::expand_path(file_name, [, dir_string])
ex
File.expand_path(‘some_file.txt’) #=> ‘/Users/anthonygiuliano/Projects/file-cms/file.txt’
ex
File.expand_path(‘some_file.txt’, ‘~’) #=> ‘/Users/anthonygiuliano/some_file.txt’
Ruby File
references the name of the current file in a Ruby program
__FILE__
Ruby File
opens the file, optionally seeks to the given offset, then returns length bytes (defaulting to the rest of the file)
File::read(name, [length [, offset]] [, opt] )
ex
file = File::read(‘some_file.txt’)
Ruby File
expands PATTERN, which is an Array of patterns or a pattern String, and returns the results as matches or as arguments given to the block
Dir::glob( pattern, [flags] ) => matches
Dir::glob( pattern, [flags] ) { |filename| block } => nil
ex
Dir.glob(‘*’) #=> Each element in this array is a string with the name and extension of a file in the current directory
Ruby File
returns the last component of the filename given in file_name
File::basename(file_name [, suffix] ) => base_name
ex
File.basename(“/home/gumby/work/ruby.rb”) #=> “ruby.rb” File.basename(“/home/gumby/work/ruby.rb”, “.rb”) #=> “ruby”
File.basename(“/home/gumby/work/ruby.rb”, “.*”) #=> “ruby”
Ruby File
deletes the named files, returning the number of names passed as arguments
File::delete(file_name, …) => integer
Ruby File
returns true if the named file is a directory, or a symlink that points at a directory, and false otherwise
File::directory?(file_name) => true or false
Ruby File
returns all components of the filename given in file_name except the last one
File::dirname(file_name) => dir_name
Ruby File
return true if the named file exists
File::exist?(file_name) => true or false
Ruby File
returns the extension (the portion of file name in path starting from the last period).
File::extname(path) => string
ex
File.extname(“test.rb”) #=> “.rb” File.extname(“a/b/d/test.rb”) #=> “.rb”
File.extname(“foo.”) #=> “”
File.extname(“test”) #=> “”
File.extname(“.profile”) #=> “”
File.extname(“.profile.sh”) #=> “.sh”
Ruby File
returns a new string formed by joining the strings using File::SEPARATOR
File::join(string, …) => string
Ruby File
opens the file named by filename according to the given mode and returns a new File object
File::new(filename, mode=”r” [, opt]) => file
ex
f = File.new(“testfile”, “r”)
f = File.new(“newfile”, “w+”)
Ruby File
with no associated block, this is a synonym for ::new; if the optional code block is given, it will be passed the opened file as an argument and the File object will automatically be closed when the block terminates; the value of the block will be returned
File::open(filename, mode=”r” [, opt]) {|file| block } => obj
Ruby File
renames the given file to the new name
File::rename(old_name, new_name) => 0
Ruby File
splits the given string into a directory and a file component and returns them in a two-element array
File::split(file_name) => array
ex
File.split(“/home/gumby/.profile”) #=> [“/home/gumby”, “.profile”]