Name for a value in memory. Retrieving values for later use.
age = 18
age # => 18
require_relative "router" | |
require_relative "task_repository" | |
require_relative "tasks_controller" | |
# Start a task repo and add a task to it | |
task_repository = TaskRepository.new | |
tasks_controller = TasksController.new(task_repository) | |
router = Router.new(tasks_controller) | |
router.run |
class Animal | |
attr_reader :name | |
def initialize(name) | |
@name = name | |
end | |
def talk | |
"#{name} #{@sound_verb}" | |
end |
# PORO | |
class Citizen | |
def initialize(first_name, last_name, age) | |
@first_name = first_name&.capitalize || "" | |
@last_name = last_name.capitalize | |
@age = age | |
end | |
def can_vote? | |
@age > 18 |
class Car | |
attr_reader :brand | |
attr_accessor :color # attr_reader / attr_writer | |
def initialize(color, brand) # initializer / constructor | |
# STATE/DATA/ATTRIBUTE: with instance variables | |
@color = color | |
@brand = brand | |
@engine_running = false | |
end |
def display_list(array) | |
array.each_with_index do |gift, index| | |
box = gift[:bought] ? "[X]" : "[ ]" | |
puts "#{index + 1}. #{box} - #{gift[:name]} - $#{gift[:price]}" | |
end | |
end | |
def ask_for(string) | |
# string => "What's the name?" | |
puts string |
# Quick data objects recap: | |
# There are 99 bottles of Indio in Cantina La Llorona. | |
# They cost $20.15 MXN each. The restaurant is currently closed. | |
# Cantina La Llorona has the following beers for sale: | |
# Leopoldina, Backbone, Dogma, Havana Dreams. | |
# Their prices are respectively: |
# Here's the refactoring for the encrypt for use with | |
# the decrypt. | |
def encrypt(message, encrypt_key = -3) | |
alphabet = ("A".."Z").to_a | |
old_characters = message.chars | |
old_characters.map do |old_character| | |
old_index = alphabet.index(old_character) | |
unless old_index.nil? | |
new_index = old_index + encrypt_key | |
new_character = alphabet[new_index % alphabet.size] |
# Declare an array literal | |
def acronymize(sentence) | |
# create empty arrays of letters | |
letters = [] | |
# separate the sentense into string | |
words = sentence.split | |
# iterate over words | |
words.each do |word| | |
# find the first letter of each word | |
# send the letter to uppercase | |
# add each letter to the array |