File Flashcards

1
Q

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

A

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’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Ruby File

references the name of the current file in a Ruby program

A

__FILE__

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Ruby File

opens the file, optionally seeks to the given offset, then returns length bytes (defaulting to the rest of the file)

A

File::read(name, [length [, offset]] [, opt] )

ex

file = File::read(‘some_file.txt’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Ruby File

returns the last component of the filename given in file_name

A

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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Ruby File

deletes the named files, returning the number of names passed as arguments

A

File::delete(file_name, …) => integer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Ruby File

returns true if the named file is a directory, or a symlink that points at a directory, and false otherwise

A

File::directory?(file_name) => true or false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Ruby File

returns all components of the filename given in file_name except the last one

A

File::dirname(file_name) => dir_name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Ruby File

return true if the named file exists

A

File::exist?(file_name) => true or false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Ruby File

returns the extension (the portion of file name in path starting from the last period).

A

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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Ruby File

returns a new string formed by joining the strings using File::SEPARATOR

A

File::join(string, …) => string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Ruby File

opens the file named by filename according to the given mode and returns a new File object

A

File::new(filename, mode=”r” [, opt]) => file

ex

f = File.new(“testfile”, “r”)

f = File.new(“newfile”, “w+”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A

File::open(filename, mode=”r” [, opt]) {|file| block } => obj

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Ruby File

renames the given file to the new name

A

File::rename(old_name, new_name) => 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Ruby File

splits the given string into a directory and a file component and returns them in a two-element array

A

File::split(file_name) => array

ex

File.split(“/home/gumby/.profile”) #=> [“/home/gumby”, “.profile”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Ruby File

returns true if ios is at end of file that means there are no more data to read

A

IO#eof? => true or false

ex

f = File.new(“testfile”)

dummy = f.readlines

f.eof? #=> true

17
Q

Ruby File

closes ios and flushes any pending writes to the operating system

A

IO#close => true or false

18
Q

Ruby File

opens the file, optionally seeks to the given offset, writes string, then returns the length written

A

IO::write(name, string, [offset], open_args ) => fixnum

19
Q

Ruby File

different ‘modes’ for opening files in Ruby

A

‘r’ # read (existing file)

‘r+’ # read / write (existing file)

‘w’ # write only (creates new file)

‘w+’ # read / write (creates new file)

‘a’ # write only (appends or creates)

‘a+’ # read / write (appends or creates)

20
Q

Ruby File

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’

A

converts relative pathname to an absolute pathname; relative paths are referenced from the current working directory of the process unless dir_string is given

21
Q

Ruby File

__FILE__

A

references the name of the current file in a Ruby program

22
Q

Ruby File

File::read(name, [length [, offset]] [, opt] )

ex

file = File::read(‘some_file.txt’)

A

opens the file, optionally seeks to the given offset, then returns length bytes (defaulting to the rest of the file)

23
Q

Ruby File

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

A

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

24
Q

Ruby File

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”

A

returns the last component of the filename given in file_name

25
# Ruby File File::delete(file\_name, ...) =\> integer
deletes the named files, returning the number of names passed as arguments
26
# Ruby File File::directory?(file\_name) =\> true or false
returns true if the named file is a directory, or a symlink that points at a directory, and false otherwise
27
# Ruby File File::dirname(file\_name) =\> dir\_name
returns all components of the filename given in file\_name except the last one
28
# Ruby File File::exist?(file\_name) =\> true or false
return true if the named file exists
29
# Ruby File 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"
returns the extension (the portion of file name in path starting from the last period).
30
# Ruby File File::join(string, ...) =\> string
returns a new string formed by joining the strings using File::SEPARATOR
31
# Ruby File File::new(filename, mode="r" [, opt]) =\> file ex f = File.new("testfile", "r") f = File.new("newfile", "w+")
opens the file named by filename according to the given mode and returns a new File object
32
# Ruby File File::open(filename, mode="r" [, opt]) {|file| block } =\> obj
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
33
# Ruby File File::rename(old\_name, new\_name) =\> 0
renames the given file to the new name
34
# Ruby File File::split(file\_name) =\> array ex File.split("/home/gumby/.profile") #=\> ["/home/gumby", ".profile"]
splits the given string into a directory and a file component and returns them in a two-element array
35
# Ruby File IO#eof? =\> true or false ex f = File.new("testfile") dummy = f.readlines f.eof? #=\> true
returns true if ios is at end of file that means there are no more data to read
36
# Ruby File IO#close =\> true or false
closes ios and flushes any pending writes to the operating system
37
# Ruby File IO::write(name, string, [offset], open\_args ) =\> fixnum
opens the file, optionally seeks to the given offset, writes string, then returns the length written
38
# Ruby File 'r' # read (existing file) 'r+' # read / write (existing file) 'w' # write only (creates new file) 'w+' # read / write (creates new file) 'a' # write only (appends or creates) 'a+' # read / write (appends or creates)
different 'modes' for opening files in Ruby