Skip to content

Instantly share code, notes, and snippets.

View profh's full-sized avatar

Larry Heimann (Prof. H) profh

  • Carnegie Mellon University
  • Pittsburgh, PA
  • X @profh
View GitHub Profile
@profh
profh / new_friend_msg.text.erb
Last active April 15, 2024 22:12
For sending a friend message as a Klingon would.
nuqneH <%= @friend.nickname %>,
I've just listed you as one of my friends. I am sure you are deeply honored, as you should be.
Remember the Klingon proverb regarding friends:
may'Daq jaHDI' SuvwI' juppu'Daj lonbe'
(When a warrior goes to a battle, he does not abandon his friends.)
Don't forget about me when you go into battle -- I will not forget you.
@profh
profh / remove_friend_msg.html.erb
Last active April 15, 2024 22:11
A mailer template for removing a friend as a Klingon would.
<p><%= @friend.full_name %>,</p>
<p>I had previously listed you as a friend; however, I am reminded of the old Klingon proverb:<br /><br />
QaghmeylIj tIchID, yIyoH <br />
<em>(Have the courage to admit your mistakes.)</em><br />
</p>
<p>Clearly it was a mistake to make you a friend. I admit this error in judgment and hereby remove you from my friend list.</p>
@profh
profh / ruby_objects.rb
Created March 15, 2022 13:44
A quick intro to Ruby object model
# Examples: looking at classes and ancestors
5.class # => Integer
5.class.ancestors # => [Integer, Numeric, Comparable, Object, Kernel, BasicObject]
"Quack".class # => String
"Quack".class.ancestors # => [String, Comparable, Object, Kernel, BasicObject]
[1,2,3].class # => Array
[1,2,3].class.ancestors # => [Array, Enumerable, Object, Kernel, BasicObject]
### A SIMPLE EXAMPLE OF BLOCKS
numbers = [1, 2, 3, 4, 5]
# .each iterates through an array or hash and performs an anonymous function on it
numbers.each { |n| puts "The square of #{n} is #{n**2}" }
# note also the string interpolation with the #{}
# note that numbers is unchanged
p numbers
puts "\n\n--------\n\n"

Notes on Enums in Rails

Enums in Rails are relatively new, but also incredibly useful. When we create an enum, we get lots of other things that come along with that for free. Consider this example from PATS Medicine model:

enum :admin_method, { oral: 1, injection: 2, intravenous: 3, topical: 4}, scopes: true

When I create this, because I have specified scopes: true, I automatically get scopes written for Medicine.oral, Medicine.injection, Medicine.intravenous, and Medicine.topical. I still need to test these methods to be sure they were created as intended, but I have confidence that they will work and help us filter medicines by the appropriate admin_method.

@profh
profh / combine_exercise.swift
Last active November 5, 2023 13:13
Combine Exercise Solution
// One solution to the class exercise
import UIKit
import Combine
import PlaygroundSupport
// This playground will execute indefinetly in order to give our
// async operations enough time to execute.
PlaygroundPage.current.needsIndefiniteExecution = true
@profh
profh / combine_demo.swift
Created October 31, 2023 11:23
Combine Demo
// h/t to @JohnSundell (https://www.swiftbysundell.com/)
import UIKit
import Combine
import PlaygroundSupport
// This playground will execute indefinetly in order to give our
// async operations enough time to execute.
PlaygroundPage.current.needsIndefiniteExecution = true
# Active Record provides a rich API for accessing data within a database.
# Below are a few examples of different data access methods provided by Active Record.
Animal.all
# return a collection with all animals
Animal.first
# return the first animal added
Animal.last
# return the last animal added
Animal.find(4)
@profh
profh / blocks_2019.rb
Created February 25, 2020 15:07
Blocks from exam 1, Spring 2019
criminal_aliases = {"Oswald Cobblepot"=>"Penguin", "Jonathan Crane"=>"Scarecrow", "Harvey Dent"=>"Two Face", "Edmund Dorrance"=>"Bane", "Thomas Elliot"=>"Hush", "Carmine Falcone"=>"The Roman", "Victor Fries"=>"Mr. Freeze", "Sophia Gigante"=>nil, "Matt Hagan"=>"Clayface", "Pamela Isley"=>"Poison Ivy", "Waylon Jones"=>"Killer Croc", "Selina Kyle"=>"Catwoman", "Kirk Langstrom"=>"Man-Bat", "Salvatore Maroni"=>nil, "Jack Napier"=>"Joker", "Edward Nigma"=>"Riddler", "Harleen Quinzel"=>"Harley Quinn", "Slade Wilson"=>"Deathstroke", "Ra's al Ghul"=>"Ra's"}
female_criminals = %w[Selina\ Kyle Harleen\ Quinzel Pamela\ Isley Sophia\ Gigante"]
convicted_felons = ["Oswald Cobblepot", "Jonathan Crane", "Harvey Dent", "Edmund Dorrance", "Thomas Elliot", "Victor Fries", "Matt Hagan", "Pamela Isley", "Waylon Jones", "Selina Kyle", "Salvatore Maroni", "Edward Nigma", "Harleen Quinzel", "Slade Wilson"]
# enhanced_powers = ["Jonathan Crane", "Edmund Dorrance", "Matt Hagan", "Pamela Isley", "Waylon Jones", "Kirk Langstrom", "S
### A Quick Base64 Encode/Decode Demo ###
# require Ruby's base64 library
require 'base64'
# Create a message (Zach Synder's Justice League cast)
heroes = %w[Batman Superman Wonder\ Woman Flash Aquaman Cyborg]
message = "Justice League: #{heroes.join(', ')}."
# Base74 encoding of the message