Skip to content

Instantly share code, notes, and snippets.

@hidori
Last active August 29, 2015 14:00
Show Gist options
  • Save hidori/e49e67473c969214583d to your computer and use it in GitHub Desktop.
Save hidori/e49e67473c969214583d to your computer and use it in GitHub Desktop.
Ruby の unlessぽいヤツ
void Main()
{
var x = 1;
Unless(x == 0, () => Console.WriteLine("A"));
Unless(x == 0, () => Console.WriteLine("A"), () => Console.WriteLine("B"));
Console.WriteLine(Unless(x == 0, () => "A"));
Console.WriteLine(Unless(x == 0, () => "A", () => "B"));
}
public static void Unless(bool condition, Action then)
{
if (!condition)
{
then();
}
}
public static void Unless(bool condition, Action then, Action @else)
{
if (!condition)
{
then();
}
else
{
@else();
}
}
public static T Unless<T>(bool condition, Func<T> then)
{
return (!condition)
? then()
: default(T);
}
public static T Unless<T>(bool condition, Func<T> then, Func<T> @else)
{
return (!condition)
? then()
: @else();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment