lines = File.readlines(“text.txt”)
line_count = lines.size
text = lines.join
puts “#{line_count} lines”
file implements a readlines method that reads an entire file into an array, line by line.
total_characters = text.length
puts “#{total_characters} characters”
characters including white spaces
open source
cource code to an application or library is made available publicly for other people to look at and use.
local variables
x = 10
puts x
can be used only in the same place it is defined.
scope
where a variable can be used
global variables
def basic_method puts $x end $x = 10 basic_method
object variables
class Square def initialize(side_length) @side_length = side_length end
class variables
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
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
Hello from the Square class! Hello from an instance of class Square!