Skip to content

Instantly share code, notes, and snippets.

View rodloboz's full-sized avatar

Rui Freitas rodloboz

View GitHub Profile
@rodloboz
rodloboz / french_ssn.rb
Created July 2, 2018 10:46
lw-livecode
require 'date'
require 'yaml'
PATTERN = /^(?<gender>[12])\s?(?<yob>\d{2})\s?(?<mob>0[1-9]|1[0-2])\s?(?<department>\d\d|2[AB])\s?\d{3}\s?\d{3}\s?(?<key>\d\d)$/
# hash
DEPARTMENTS = YAML.load_file('./data/french_departments.yml')
def french_ssn(ssn)
return "The number is invalid" if ssn.empty?
# Player begins with 100$
# if player wins 50$
# otherwise loses 10$
# player = { wallet: 100 }
# list of a horses / race roster => Array
# ==== START LOOP ====
# display list of horses
# ask the user for the winning horse
# store user choice
@rodloboz
rodloboz / acronymize.rb
Created June 28, 2018 09:59
Acronymize with #map
def acronymize(sentence)
# map => []
# applies transformations to elements
# 1. split sentence into words (list use Array)
words = sentence.split
# 2. Go through/Take each word
# and take/grab the first letter (capitalize)
initials = words.map { |word| word[0].capitalize }
# 3. Join words into a single sentence
# Ranges
range_1 = (1..10).to_a # includes the last element
p range_1
range_2 = (1...10).to_a # excludes the last element
p range_2
# Arrays
@rodloboz
rodloboz / acronymize.rb
Created June 27, 2018 10:37
Acronymize
def acronymize(string)
# TODO: write a method that returns
# the acronym for the given string
# 1. Split the string into words
# 2. Take the first letter of each word
# & capitalize them
# 3. Join the first letters together
# 4. Return the result
words = string.split(" ")
@rodloboz
rodloboz / black_jack.rb
Created June 27, 2018 10:36
Black Jack
def pick_bank_score
# TODO: Use Random to get a new random score
# rand(16...21) doesn't include 21
rand (16..21) # includes 21
end
def pick_player_card
# TODO: Use Random to get a new random card
rand(1..11)
end
puts "How old are you?"
age = gets.chomp.to_i
# Conditional Statements
# if / else structure
if age >= 18
puts "You can vote!"
else
require 'date'
def days_until_xmas(date = Date.today)
# TODO: calculate the number of days
# until Christmas
current_year = Time.now.year
if (Date.new(current_year, 12, 25) - date).to_i < 0
(Date.new(current_year + 1, 12, 25) - date).to_i
else
(Date.new(current_year, 12, 25) - date).to_i
# "Hello" # string
# true # boolean
# false # boolean
# 3 # integer
# 3.14 # float
# ["Hello", 3, 3.14, true] # array
# (1..10) # range
# # String interpolation
@rodloboz
rodloboz / patient.eb
Created May 1, 2018 10:09
Modeling Data
class Patient
attr_reader :name, :cured
attr_accessor :id, :room # attr_red + attr_writer
# STATE:
# - name (String)
# - cured (Boolean)
# STRATEGY 1
# def initialize(name, cured)