Created
July 25, 2015 19:15
-
-
Save ryanbraganza/139b14aa3ba1ba2ad3ec to your computer and use it in GitHub Desktop.
partial tswift 22
This file contains hidden or 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
| module Deferredness | |
| def defer phrase | |
| thread = Thread.new do | |
| sleep 0.0001 # FIXME hax | |
| puts ' if ' + phrase | |
| end | |
| threads << thread.run | |
| end | |
| def await | |
| threads.map(&:join) | |
| self.threads = [] | |
| end | |
| def threads= threads | |
| @threads = threads | |
| end | |
| def threads | |
| @threads ||= [] | |
| end | |
| end | |
| class TwentyTwo | |
| include Deferredness | |
| attr_accessor :it, :to, :we, :this_place, :tonight | |
| def initialize | |
| self.it = Pronoun.new(:it) | |
| self.to = Pronoun.new(:to) | |
| self.we = Pronoun.new(:we) | |
| self.this_place = Pronoun.new(:this_place) | |
| self.tonight = Pronoun.new(:tonight) | |
| end | |
| def play | |
| it.feels_like :a_perfect_night | |
| to.dress_up_like :hipsters and make_fun_of :our_exes | |
| repeat 'uh uh' | |
| take_a_breath | |
| it.feels_like :a_perfect_night, :for => Breakfast.at(:midnight) | |
| to.fall_in_love_with :strangers | |
| repeat 'uh uh' | |
| take_a_breath | |
| bridge | |
| chorus | |
| it.seems_like :one_of_those_nights | |
| this_place.is Phrase.new.too('crowded') | |
| puts <<-EOS | |
| Too many cool kids, uh uh, uh uh (who's Taylor Swift anyway, ew?) | |
| It seems like one of those nights, | |
| We ditch the whole scene and end up dreaming | |
| Instead of sleeping. | |
| EOS | |
| take_a_breath | |
| bridge | |
| end | |
| private | |
| def bridge | |
| exclaim 'yeah' | |
| take_a_breath | |
| we.are %w[happy free confused lonely], :at => :the_same_time | |
| it.is %w[miserable magical] | |
| exclaim 'oh yeah' | |
| take_a_breath | |
| tonight.is Phrase.new.the.night.when.we.forget.about.the('deadlines') | |
| it.is ['time'] | |
| take_a_breath | |
| end | |
| def chorus | |
| but = Conjunction.new(:but) | |
| puts Phrase.new.I.dont.know.about('you') | |
| but[->(){ Pronoun.new(:I).feels_like('22') }] | |
| print Phrase.new.Everything.will.be('alright') if defer 'you_keep_me_next_to_you' | |
| await | |
| You.know_about? :me | |
| but[->(){ Pronoun.new(:I).bet_you('want to') }] | |
| print "Everything will be alright" if defer "we just keep dancing like we're" | |
| await | |
| repeat '22, ooh-ooh', separator: "\n" | |
| take_a_breath | |
| end | |
| def take_a_breath | |
| puts | |
| end | |
| def make_fun_of what_to_make_fun_of | |
| puts "make fun of #{what_to_make_fun_of}" | |
| end | |
| def repeat what_to_repeat, opts={:separator => ', ', :times => 2} | |
| separator = opts[:separator] || ', ' | |
| times = opts[:times] || 2 | |
| puts ([what_to_repeat] * times).join separator | |
| end | |
| def exclaim word, continuation=5 | |
| puts word.capitalize.sub(/(.$)/, '\1' * continuation) + '!' | |
| end | |
| end | |
| class Breakfast | |
| def self.at time | |
| "#{name} at #{time}" | |
| end | |
| end | |
| Pronoun = Struct.new(:name) do | |
| [ | |
| :feels_like, | |
| :seems_like, | |
| :fall_in_love_with, | |
| :make_fun_of, | |
| :dress_up_like, | |
| :bet_you, | |
| ].each do |verb| | |
| define_method verb do |noun_phrase, opts={}| | |
| do_thing verb.to_s.gsub('_', ' '), noun_phrase, opts | |
| end | |
| end | |
| def is things | |
| puts "#{name} is #{describe things}" | |
| end | |
| def are things, opts | |
| puts "#{name} are #{describe things}#{describe opts}" | |
| end | |
| private | |
| def do_thing verb_phrase, noun_phrase, opts={} | |
| puts "#{name} #{verb_phrase} #{noun_phrase}#{describe opts}" | |
| self | |
| end | |
| def describe things={} | |
| case things | |
| when Hash then | |
| things.reduce('') do |s, (k, v)| | |
| s + " #{k} #{v}" | |
| end | |
| when Array then | |
| things.each.with_index.reduce('') do |s, (thing, index)| | |
| s + if index == 0 | |
| thing | |
| elsif index < things.length - 1 | |
| ", #{thing}" | |
| else | |
| " and #{thing}" | |
| end | |
| end | |
| else | |
| things.to_str | |
| end | |
| end | |
| end | |
| class Phrase | |
| def initialize | |
| @phrase = '' | |
| end | |
| def respond_to? sym, *args | |
| return false if sym == :to_ary | |
| super | |
| end | |
| def method_missing sym, *args | |
| @phrase += sym.to_s + ' ' + args.join | |
| self | |
| end | |
| def to_str | |
| @phrase | |
| end | |
| def to_s | |
| to_str | |
| end | |
| end | |
| class You | |
| @@knows_about = [] | |
| class << self | |
| def know_about? thing | |
| if @@knows_about.include? thing | |
| puts "#{self} knows about #{thing}" | |
| else | |
| puts "#{self} don't know about #{thing}" | |
| end | |
| end | |
| end | |
| end | |
| Conjunction = Struct.new(:name) do | |
| def [] phrase | |
| print "#{name} " | |
| phrase.() | |
| end | |
| end | |
| TwentyTwo.new.play |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment