Skip to content

Instantly share code, notes, and snippets.

View rodloboz's full-sized avatar

Rui Freitas rodloboz

View GitHub Profile
@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
@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(" ")
# 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 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
# 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 / 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?
@rodloboz
rodloboz / gist:1ca63ac74b2be73867d1af42a6f37eea
Created October 5, 2018 06:45
Triggering Sweet Alert with Rails remote: true
# add remote: true to form
<%= simple_form_for @model, remote: true do |f| %>
# ...
<%= f.button :submit %>
<% end %>
# controller receives ajax request
def create
# do something
def acronymize(string)
# TODO: write a method that returns
# the acronym for the given string
# 1. Split the sentence into words
# 2. Grab the first letter of each word
# 3. Capitalize it
# 4. Join the first letters together
# 5. return the result
# gsub: string to replace , replace with
# YIELD
def bigcount
(0..100000000).sort
end
def timer(callback)
start_time = Time.now
def encrypt(sentence, permutation_level = -3)
return "" if sentence.empty?
alphabet = ("A".."Z").to_a
# 1. Split string into letters
letters = sentence.upcase.split("")
# 2. Go through each letter
result = ""
# letters.each do |letter|
# # 3. Find index in list of letters
# letter_index = alphabet.index(letter)