Files Flashcards
1
Q
lines = File.readlines(“text.txt”)
line_count = lines.size
text = lines.join
puts “#{line_count} lines”
A
file implements a readlines method that reads an entire file into an array, line by line.
2
Q
total_characters = text.length
puts “#{total_characters} characters”
A
characters including white spaces
3
Q
open source
A
cource code to an application or library is made available publicly for other people to look at and use.
4
Q
local variables
A
x = 10
puts x
can be used only in the same place it is defined.
5
Q
scope
A
where a variable can be used
6
Q
global variables
A
def basic_method puts $x end $x = 10 basic_method
7
Q
object variables
A
class Square def initialize(side_length) @side_length = side_length end
8
Q
class variables
A
class Square def initialize if defined?(@@number_of_squares) @@number_of_squares += 1 else @@number_of_squares = 1 end end def self.count @@number_of_squares end end
a = Square.new
b = Square.new
puts Square.count
ouput : 2
9
Q
class Square def self.test_method puts "Hello from the Square class!" end
def test_method puts "Hello from an instance of class Square!" end end Square.test_method Square.new.test_method
A
Hello from the Square class! Hello from an instance of class Square!