Skip to content

Instantly share code, notes, and snippets.

View hrs's full-sized avatar

Harry R. Schwartz hrs

View GitHub Profile
@hrs
hrs / brainfuck.rb
Created June 18, 2014 18:25
Brainfuck interpreter
class Brainfuck
attr_reader :data, :data_pointer, :instruction_pointer, :code
def initialize(code)
@data = Hash.new(0)
@data_pointer = 0
@instruction_pointer = 0
@code = code
end
@hrs
hrs / zeller.rb
Created June 19, 2014 16:57
Implementation of Zeller's algorithm
def weekday(day, month, year)
day_names = %w(Saturday Sunday Monday Tuesday Wednesday Thursday Friday)
century = (year.to_f / 100.0).floor
year_of_century = year % 100
month += 12 if month < 3
h = (day +
((13 * (month + 1)) / 5.0).floor +
year_of_century +
(year_of_century / 4.0).floor +
@hrs
hrs / intro-to-emacs-lisp.el
Created June 19, 2014 17:02
Code used in my Emacs Lisp talk
;;; An Introduction to Emacs Lisp
;;; Harry Schwartz, 2014
;; What we'll be covering:
'(atoms
functions
lists
variables
defining-functions
@hrs
hrs / decks.txt
Created June 19, 2014 17:26
Decks for the flashcard problem
2
french
2
chat
cat
papillon
butterfly
spanish
2
gato
@hrs
hrs / wombat.rb
Created June 22, 2014 15:04
Illustrate differences between private and public
# The key difference between public and private methods is that you
# can call public methods on instances of the class, but you can't
# call private methods on instances of the class.
# You can only call private methods *inside* the class. It's fine for
# a public method to call a private method, but you can't call a
# private methods directly on an instance.
class Wombat
def eat_bacon
@hrs
hrs / named_wombat.rb
Last active August 29, 2015 14:02
Using attr_readers, attr_writers, and attr_accessors
# attr_reader/writers/accessors are handy shortcuts for creating
# getter and setter methods. That's all they're for.
# An attr_reader creates a getter method. For example, the following
# two classes have identical effects:
class NamedWombat
attr_reader :name
def initialize(name)
### Keybase proof
I hereby claim:
* I am hrs on github.
* I am hrs (https://keybase.io/hrs) on keybase.
* I have a public key whose fingerprint is 1B41 8F2C 23DE DD9C 807E A74F 841B 3DAE 25AE 721B
To claim this, I am signing this object:
@hrs
hrs / pi.rb
Created July 17, 2014 16:38
Probabilistic calculation of pi in 56 characters
p (0..1000000).count{rand<Math.sqrt(1-rand**2)}/250000.0
@hrs
hrs / say_repl.rb
Created July 21, 2014 01:25
REPL for Say
#!/usr/bin/env ruby
class SayRepl
attr_reader :voice
def initialize(voice)
@voice = voice
end
def go
@hrs
hrs / process_tree.rb
Created January 16, 2015 22:36
Generate a graph of the process tree
#!/usr/bin/env ruby
class UnixProcess
attr_reader :uid, :pid, :ppid, :cmd
def initialize(uid:, pid:, ppid:, cmd:)
@uid = uid
@pid = pid
@ppid = ppid
@cmd = cmd