Skip to content

Instantly share code, notes, and snippets.

@stefanoc
Created November 18, 2008 13:14
Show Gist options
  • Save stefanoc/26120 to your computer and use it in GitHub Desktop.
Save stefanoc/26120 to your computer and use it in GitHub Desktop.
class Aif
def initialize(cond)
@cond = cond
end
def else
yield unless @cond
end
end
class Object
def aif(cond)
yield(cond) if cond
Aif.new(cond)
end
end
if $0 == __FILE__
str = "foo bar"
aif(/(.+) (.+)/.match(str)) do |it|
puts "first element: #{it[1]}"
puts "second element: #{it[2]}"
end.else do
puts "no match"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment