class Object
Constants
- APPEND_MODE
- NEW_LINE
Backslash Notation
- READ_MODE
File Modes
- SCRIPT_NAME
- SHEBANG_PATH
Misc
- SPACE
- TAB
- WRITE_MODE
Public Instance Methods
ACCESS(file: '', mode: READ_MODE) { |f| ... }
click to toggle source
Opens a “file” on “READ_MODE” by default.
# File lib/r7.rb, line 36 def ACCESS(file: '', mode: READ_MODE) File.open(file, mode) { |f| yield(f) } end
APPEND(file: '', mode: APPEND_MODE, contents: '')
click to toggle source
Writes “contents” to the end of “file”.
# File lib/r7.rb, line 56 def APPEND(file: '', mode: APPEND_MODE, contents: '') WRITE(file: file, mode: mode, contents: contents) end
GET_BASENAME(filename)
click to toggle source
Returns the basename of the path, and the “filename”.
# File lib/r7.rb, line 31 def GET_BASENAME(filename) File.basename(filename, GET_EXTNAME(filename)) end
GET_EXTNAME(filename)
click to toggle source
Returns the file extention for the “filename”.
# File lib/r7.rb, line 26 def GET_EXTNAME(filename) File.extname(filename) end
PLACE(this_content: '', on_this_line: 1, in_this_file: '')
click to toggle source
Returns a new string with content add in place. @example
# This is in a file named: foobar.rb $foobar = true # This is in an file named: replace_foobar.r7 # We'll make "foobar" into an instance variable instead... PLACE(this_content: '@foobar = true', on_this_line: 10, in_this_file: 'foobar.rb') # Results @foobar = true
# File lib/r7.rb, line 69 def PLACE(this_content: '', on_this_line: 1, in_this_file: '') offset = index_offset(on_this_line) contents = READ(file: in_this_file) contents = contents.split(NEW_LINE) contents.each_index { |i| contents[index_offset(i)] = this_content if i == on_this_line } # This corrects an error that occurs when there is no string that follows the index intended to be modified. contents[index_offset(on_this_line)] = this_content unless contents[index_offset(on_this_line)] == this_content contents.join(NEW_LINE) end
READ(file: '')
click to toggle source
Returns a string with all of the files contents.
# File lib/r7.rb, line 41 def READ(file: '') ACCESS(file: file) do |f| f.seek(0, :END) file_size = f.tell f.rewind f.read(file_size) end end
REPLACE(this: '', with_this: '', on_this_line: 1, in_this_file: '')
click to toggle source
Returns a new string with content replacing a substring. @example
# This is in a file named: foobar.rb class FooBar end # This is in an file named: replace_foobar.r7 # Here we are turning the class into a method definition! REPLACE(this: 'class', with_this: 'def', on_this_line: 1, in_this_file: 'foobar.rb') # Results def FooBar end
# File lib/r7.rb, line 90 def REPLACE(this: '', with_this: '', on_this_line: 1, in_this_file: '') offset = index_offset(on_this_line) contents = READ(file: in_this_file) contents = contents.split(NEW_LINE)[offset].split contents.each_index { |i| contents[i] = with_this if contents[i].include?(this) } contents = contents.join(SPACE) PLACE(this_content: contents, on_this_line: on_this_line, in_this_file: in_this_file) end
WRITE(file: '', mode: WRITE_MODE, contents: '')
click to toggle source
Writes “contents” to “file”.
# File lib/r7.rb, line 51 def WRITE(file: '', mode: WRITE_MODE, contents: '') ACCESS(file: file, mode: mode) { |f| f.write(contents) } end
index_offset(index)
click to toggle source
This is needed to get the line number(start at 1), from the index(starts at 0).
# File lib/r7.rb, line 21 def index_offset(index) index - 1 end