Skip to content

Instantly share code, notes, and snippets.

View Ejhfast's full-sized avatar

Ethan Fast Ejhfast

View GitHub Profile
import empath
import sys
lexicon = empath.Empath()
trump_speech = open(sys.argv[1], "r").read()
obama_speech = open(sys.argv[2], "r").read()
trump_categories = lexicon.analyze(trump_speech, normalize=True)
obama_categories = lexicon.analyze(obama_speech, normalize=True)
@Ejhfast
Ejhfast / near_miss.rb
Last active December 20, 2015 20:59
Some sort-of-cool near miss examples
# 1: Implicit return of each block is thrown away
# => You probably want some kind of state change
list_of_lists = [
["Ethan Fast", nil, "Lucy Wang", "Daniel Steffee"],
[2007, 2011, 2010, nil]
]
list_of_lists.each do |l|
l.compact # You probably meant compact!, which appears once.
end
@Ejhfast
Ejhfast / after_taazr_redundant.html
Created August 4, 2011 15:21
After taazr redundancy init
<script src="/static/proxino.js" type="text/javascript"></script>
<script>
Proxino.secret = "eacc9c30-9b68-012e-dcf5-12313d1c9482";
Proxino.connect('/static/logged_in.js','/static/test.js');
</script>
@Ejhfast
Ejhfast / before_taazr_redundant.html
Created August 4, 2011 15:18
Before taazr redundancy initialization
<script src="/static/logged_in.js" type="text/javascript"></script>
<script src="/static/test.js" type="text/javascript"></script>
@Ejhfast
Ejhfast / mix.rb
Created December 29, 2010 16:46
mix and fstore
def mix(s1, s2, s3, &block)
name = "#{s1.to_s}_#{s2.to_s}_#{s3.to_s}".to_sym
self.send(name) {|a,b| send(s1, send(s2,a), send(s3,b))}
end
@Ejhfast
Ejhfast / method-missing.rb
Created December 29, 2010 16:32
method missing
def method_missing(symbol, &block)
if block_given?
self.class.send :define_method, symbol, &block
else
bdy = proc {|*x| symbol}
self.class.send :define_method, symbol, bdy
end
@dynamic_methods << symbol
symbol
end
@Ejhfast
Ejhfast / fstore-mix.rb
Created December 29, 2010 16:14
mix in fstore
# Mix a few methods
e.mix(:plus, :double, :triple)
# => create: def plus_double_triple a,b ; plus (double a) (triple b) ; end
# We can call the new method
e.plus_double_triple 2, 3 #=> (plus (double 2) (triple 3)) => 13
@Ejhfast
Ejhfast / send.rb
Created December 29, 2010 16:07
Ruby send usage
# This is what we did
self.class.send :define_method, symbol, &block
# This, although seemingly equivalent, would not work
# because define_method is private
define_method symbol, &block
# Another example of using send
[1,2,3].first # is equivalent to
[1,2,3].send(:first)
# New object
e = FStore.new
# Define a bunch of methods dynamically
e.double {|x| 2*x}
e.triple {|x| 3*x}
e.square {|x| x**2}
e.plus {|x,y| x + y}
# Then you can use them, like so:
class FStore
attr_accessor :dynamic_methods
def initialize
@dynamic_methods = []
end
def mix(s1, s2, s3, &block)
name = "#{s1.to_s}_#{s2.to_s}_#{s3.to_s}".to_sym
self.send(name) {|a,b| send(s1, send(s2,a), send(s3,b))}
end
def method_missing(symbol, &block)