Skip to content

Instantly share code, notes, and snippets.

@ryanckulp
ryanckulp / hash_actions.rb
Created January 1, 2018 22:54
adding key/values to a Hash in Ruby
questions = {} # => create an empty Hash/object
questions['one'] = "What's your name?"
questions['two'] = "How old are you?"
@ryanckulp
ryanckulp / eccentric_array.rb
Created January 1, 2018 23:07
array with different element types in Ruby
arr = [3, "four", {five: 'six'}]
@ryanckulp
ryanckulp / student_age_array.rb
Created January 1, 2018 23:14
array of floats for looping in Array
ages = [16, 15.6, 16.25, 17.1, 16.4, 15.8]
total_years = 0.0
ages.each do |age|
total_years += age
end
total_years / ages.size # => 16.191666666666666
@ryanckulp
ryanckulp / grocery_array.rb
Last active January 2, 2018 01:33
looping through an array of groceries in Ruby
groceries = ["apple", "pear", "banana"]
groceries.each do |grocery|
puts grocery
end
# =>
# apple
# pear
# banana
@ryanckulp
ryanckulp / flash_cards.rb
Last active January 1, 2018 23:41
a sample flash card application in Ruby
questions = {
'one' => {text: "What is 2 + 2?", answer: 4},
'two' => {text: "What is 4 + 7?", answer: 11},
'three' => {text: "What is 7 + 3?", answer: 10},
}
user_answers = {
'one' => 4,
'two' => 9,
'three' => 10
@ryanckulp
ryanckulp / if_guard.rb
Created January 2, 2018 03:55
example if/guard clause in Ruby
# verbose
if 2 + 2 == 4
puts "Correct!"
end
# short-hand
puts "Correct" if 2 + 2 == 4
# more realistic short-hand
def addition_problem(user_answer, correct_answer)
@ryanckulp
ryanckulp / unless.rb
Created January 2, 2018 03:58
example unless statement in Ruby
unless 2 + 2 == 4
puts "Incorrect!"
end
@ryanckulp
ryanckulp / unless_good.rb
Last active January 2, 2018 04:09
intuitive use case for unless...end statement in Ruby
require 'date'
def date_night?
Date.today.wday == 5 # friday
end
unless date_night?
puts "study hard!"
end
@ryanckulp
ryanckulp / jp_morgan.rb
Created February 27, 2018 20:31
sanitizes JP morgan string variants to singular intention
str_a = 'j.p. morgan'
str_b = 'jp morgan chase'
str_a == str_b # => false
str = 'j.p. morgan'
str.downcase.gsub("j.p.", "jp").gsub("j.p", "jp").gsub("jp.", "jp").gsub("jp morgan", "jp morgan chase") # => "jp morgan chase"
def jp_morgan_chase?(str)
brew install yarn
rails new sample_app
cd sample_app
rails s
# visit http://localhost:3000 in your browser
rm -rf sample_app