Skip to content

Instantly share code, notes, and snippets.

@rinaldifonseca
Forked from jacksonwillis/s.rb
Created April 11, 2012 02:17
Show Gist options
  • Save rinaldifonseca/2356411 to your computer and use it in GitHub Desktop.
Save rinaldifonseca/2356411 to your computer and use it in GitHub Desktop.
Sentences as Ruby code
class S;def initialize *w;@s=w;end;def method_missing *w;@s<<w;self ###########
end;def to_ary;[@s.map{|e|e=~/[\,\.\:\-\(\)\/\'\"]/?[e]:[" ",e]}.join.strip] ##
end;end;def Object.const_missing(c);S.new c;end ####### gist.github.com/2354740
puts This.is.a.sentence.represented.by.a.Ruby.expression(".") #################
#!/usr/bin/env ruby
# https://gist.github.com/2354740
class Sentence
def initialize(*words)
@sentence = words.flatten
end
def method_missing(*words)
self.class.new(@sentence << words)
end
def to_ary
[@sentence.map { |el| el =~ /[\,\.\:\-\(\)\[\]\{\}\/\\\|\'\"]/ ? [el] : [" ", el] }.join.strip]
end
end
def Object.const_missing(const)
Sentence.new const
end
puts One.small.step.for.a.man(",").a.giant.leap.for.mankind!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment