Skip to content

Instantly share code, notes, and snippets.

@sparkertime
Created July 30, 2014 19:27
Show Gist options
  • Save sparkertime/3ba08c9739e288da3d76 to your computer and use it in GitHub Desktop.
Save sparkertime/3ba08c9739e288da3d76 to your computer and use it in GitHub Desktop.
When a Rubyist writes C#...
using System;
public static class GlobalExtensions {
public static bool IsNull<T>(this T self) {
return self == null;
}
public static bool IsPresent<T>(this T self) {
return !self.IsNull();
}
public static void Try<T>(this T self, Action<T> action) {
if(self.IsPresent()) {
action(self);
}
}
public static X Try<T,X>(this T self, Func<T,X> function) {
return self.Try(function, default(X));
}
public static X Try<T,X>(this T self, Func<T,X> function, X defaultValue) {
if(self.IsPresent()) {
return function(self);
}
return defaultValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment