Files Flashcards
include
allows you to include code defined in the module and insert it into a class ex. module Log def class_type “This class is of type: #{self.class}” end end class TestClass include Log # … end tc = TestClass.new.class_type
load
similar to require load is there if you want a library to be loaded each time load is called. ex. load ‘test_library.rb’
require
The require method allows you to load a library and prevents it from being loaded more than once. The require method will return ‘false’ if you try to load the same library after the first time. The require method only needs to be used if library you are loading is defined in a separate file ex. require ‘test_library’
extend
When using the extend method instead of include, you are adding the module’s methods as class methods instead of as instance methods. ex. module Log def class_type “This class is of type: #{self.class}” end end class TestClass extend Log # … end tc = TestClass.class_type prints This class is of type: TestClass
include
allows you to include code defined in the module and insert it into a class ex. module Log def class_type “This class is of type: #{self.class}” end end class TestClass include Log # … end tc = TestClass.new.class_type
File.open( )
opens specified file
.read
reads file as a string
.truncate
deletes contents of file ex. filename.truncate
.close( )
closes the file ex. filename.close()
.write(stuff)
writes content to file ex. line1= “Hello World!” file.write(line1)