-
-
Save jacksonwillis/2354740 to your computer and use it in GitHub Desktop.
Sentences as Ruby code
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 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! ##### |
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
#!/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! |
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
#!/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.") } |
I thought about doing it that way, but since this is just for show, I decided to do it in a more "pure" functional way.
Ah, I see what you mean. Returning 'self' is functional too, though.
but because you modify a variable, you lose the benefit of being functional
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
Don't create so many objects, check out my method_missing here: https://gist.github.com/2359584