Skip to content

Instantly share code, notes, and snippets.

View rodloboz's full-sized avatar

Rui Freitas rodloboz

View GitHub Profile
@rodloboz
rodloboz / 01-arrays.rb
Created January 17, 2019 10:46
Ruby 03-Array-Blocks Lecture
# Ranges
range_1 = (1..10) # includes the last
p range_1
range_2 = (1...10) # does not include the last
p range_2.to_a
# Arrays
movies_stars = [
@rodloboz
rodloboz / acronymize.rb
Created January 16, 2019 17:43
Ruby 04-Flow-Control Livecode
# What is an acronym:
# WTF => what the f*ck
# CIA => central intelligence agency
# FBI => federal bureau of investigation
# Pseudocode:
# 1) we start with a String
# 2) split the sentence into words => Array
# delete ["of", "the", "a"]
# 3) get the first letter of each word #each/map
@rodloboz
rodloboz / 01-recap.rb
Created January 16, 2019 11:12
Ruby 04-Flow-Control Lecture
# Data Types / Built-in Objects
"Hello" # String
'Hello' # String
4 # Integer
4.3 # Float
false # Boolean
p [3, "Hello", 3.4, true] # Array
require_relative 'person'
# ARRAY CRUD
# CREATE
# index. 0. 1. ...
musicians = ["paul", "ringo"]
p musicians
# READ
class Student
attr_reader :first_name, :last_name, :nationality, :gender
#. first_name, last_name ...
def initialize(params = {})
@first_name = params[:first_name]
@last_name = params[:last_name]
@nationality = params[:nationality]
@gender = params[:gender]
end
#. 0. 1
students = [
["john", 28],
["mary", 22],
["peter", 18]
]
# TODO: transform student into an array of hashes
students_hash = students.map do |student|
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)
# YIELD
def bigcount
(0..100000000).sort
end
def timer(callback)
start_time = Time.now
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
@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