Skip to content

Instantly share code, notes, and snippets.

# Mini challenges for numbers:
def hours_in_a_year
365 * 24
end
def minutes_in_a_decade
10 * 365 * 24 * 60
end
# bridge = Book.new
# bridge.title = "bridge to terabithiya" # writes the attribute @title
# bridge.title # makes a call to the title method that belongs to Book class
class Book
attr_writer :title # "bridge to terabithiya"
def title
lowercase = ["and", "the", "to", "a", "an", "in", "of"]
returning = []
def city_suggestion(area=nil)
cities = {
"East Coast"=>["New York", "D.C.", "Boston"],
"West Coast"=>["San Francisco", "Seattle", "Portland"],
"Central"=>["Austin", "Nashville", "Salt Lake City"]
}
if area.nil? || !cities.keys.include?(area)
puts "Hmm...have you considered #{cities.values.flatten.sample}?"
else
puts "The #{area} area is great, how about #{cities[area].sample}?"
# example word: "puppy"
def count_letters(word)
totals = word.chars.group_by(&:itself)
# totals == {"p"=>["p", "p", "p"], "u"=>["u"], "y"=>["y"]}
totals.dup.each do |key, value|
totals[key] = value.length
# e.g. totals["p"] = 3
end
def recursive_palindrome(word)
return true if word.length == 1
if word.length == 2 && word.chars.uniq.length == 1
true
elsif word.chars.first == word.chars.last
subword = word[1..-2]
return true if recursive_palindrome(subword)
false
elsif word.chars.first != word.chars.last
def recursive_factorial(num)
return 1 if num == 1 || num == 0
num * recursive_factorial((num - 1))
end
p recursive_factorial(4) # prints: 24
p recursive_factorial(1) # prints: 1
require 'date'
def deadline_date_counter(year, month, day)
end_date = Date.parse("#{year}-#{month}-#{day}")
puts "You have #{(end_date - Date.today).to_i} days left!"
end
deadline_date_counter(2016, 03, 22) # prints: "You have 20 days left!"
require 'date'
def deadline_hour_counter(year, month, day, hour)
puts "(FYI, this counter uses the 24 hr clock.)"
end_date = Time.new(year, month, day, hour)
minutes_left = (end_date - Time.now).to_i / 60
if minutes_left > 59
hours_left = minutes_left / 60
minutes_left -= (hours_left * 60)
def print_data
total_thoughts = thoughts.keys.length
puts "Total thoughts over this session: #{total_thoughts}"
inbetween = thoughts.keys
inbetween = translate_time(inbetween.inject(:+) / total_thoughts)
puts "Average time between thoughts: #{inbetween}"
best_time = thoughts[thoughts.keys.max]
puts "Longest interval between thoughts: #{best_time}"
require "pdfkit"
def generate_pdf
page = PDFKit.new(File.open("page.html"), :page_size => 'Letter')
page.to_file("page.pdf")
end