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 / 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 / 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 / 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

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.

### 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"
@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 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
@profh
profh / nlp_parts_of_speech.swift
Created November 9, 2021 21:09
Contents of playground for NLP example (parts of speech and language recognition)
// Playground using NSLinguisticTagger to analyze and tag a block of text
// https://developer.apple.com/documentation/foundation/nslinguistictagger/identifying_parts_of_speech
import Foundation
// text from some article on Democratic candidate preferences (Nov 2018)
let text = "The Democratic frontrunner, according to Politico's poll: Joe Biden, former Vice President and Senator from Delaware, who managed to grab just over a quarter (26%) of the Democrats' vote for who they'd most like to see facing off against Trump in two years for control of the White House. The runner-up is Vermont Senator Bernie Sanders, who ran a close primary campaign against Hillary Clinton in 2016, managing to get about a fifth of the votes (19%). The third-place candidate is Rep. Beto O’Rourke from Texas, who built national name-recognition through his losing Senate bid last week, with 8 percent. Following O’Rourke are three senators, all thought to be likely candidates: Sens. Elizabeth Warren (Mass.) at 5 percent, Kamala Harris
@profh
profh / Fizzbuzz.swift
Last active September 2, 2021 01:27
Fizzbuzz Playground Starter
/**
Pair up with another person to complete this assignment.
Create a Swift function called fizzbuzz() that takes an integer and returns either:
- “Fizz” if the number is divisible by 3,
- “Buzz” if divisible by 5,
- “FizzBuzz” if divisible by both 3 and 5, or
- the number itself as a string if none of these other conditions hold.
Below you can find a sample of the function being called and what is being returned.