Skip to content

Instantly share code, notes, and snippets.

@undecided
Created August 31, 2012 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save undecided/3551304 to your computer and use it in GitHub Desktop.
Save undecided/3551304 to your computer and use it in GitHub Desktop.
How do I do this?
# Premise: a chainable DSL where evaluating a section in an if statement calls valid?
# Example: Each item changes the current synonym, it tests it against
class Moggy
SYNONYMS = [:cat, :moggy, :pussycat, :feline]
def set(syn)
@synonym = syn
self
end
def valid?
SYNONYMS.include? @synonym
end
end
m = Moggy.new
m.set(:pussycat).set(:dog).valid? #=> false
if(m)
raise "Fail" # is there any way of preventing this result?
else
"Correct!"
end
@undecided
Copy link
Author

In case you were expecting a real-world example, I would like to be able to do this

foo.matches?(/some regex/).and_also(:foo => /bar/).exactly(1).times #=> true
foo.matches?(/some regex/).and_also(:foo => /bar/) #=> Want this to evaluate to true in an if statement

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