This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="/static/logged_in.js" type="text/javascript"></script> | |
<script src="/static/test.js" type="text/javascript"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
NewerOlder