Skip to content

Instantly share code, notes, and snippets.

@jacksonwillis
Created April 10, 2012 21:33
Show Gist options
  • Save jacksonwillis/2354740 to your computer and use it in GitHub Desktop.
Save jacksonwillis/2354740 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; ###### https://gist.github.com/2354740
puts This.is.a.sentence.represented.by.a.Ruby.expression(",").try.it.out! #####
#!/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_s
@sentence.map { |el| el =~ /[\,\.\:\-\(\)\/\'\"]/ ? [el] : [" ", el] }.join.strip
end
def to_ary; [to_s] end
end
def Object.const_missing(const)
Sentence.new const
end
puts One.small.step.for.a.man(",").a.giant.leap.for.mankind!
#!/usr/bin/env ruby
# https://gist.github.com/2354740
require "testrocket"
require "sentence"
+-> { One.small.step.for.a.man(",").a.giant.leap.for.mankind!.to_s.eql?("One small step for a man, a giant leap for mankind!") }
+-> { Women.belong.in.the.house("...").and.the.Senate(".").eql?("Women belong in the house... and the Senate.") }
@ptn
Copy link

ptn commented Apr 12, 2012

Ah, I see what you mean. Returning 'self' is functional too, though.

@seydar
Copy link

seydar commented Apr 12, 2012

but because you modify a variable, you lose the benefit of being functional

@jacksonwillis
Copy link
Author

Yeah, I have no idea why I said that. I wrote the code that way because I think it looks cooler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment