Created
June 22, 2012 18:02
-
-
Save davecowart/2974266 to your computer and use it in GitHub Desktop.
C# implementation of Ruby's tap method
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
public static class TapExtension { | |
public static T Tap<T>(this T obj, Action<T> block) { | |
block.Invoke(obj); | |
return obj; | |
} | |
} | |
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
var stringBuilderSmall = new StringBuilder().Tap(sb => sb.AppendLine("Inside the small tap")); | |
Console.WriteLine(stringBuilderSmall.ToString()); | |
var stringBuilderBig = new StringBuilder().Tap(sb => { | |
sb.AppendLine("Inside the big tap"); | |
sb.AppendLine("Still inside the big tap"); | |
sb.AppendLine("Yep, still inside the big tap"); | |
}); | |
Console.WriteLine(stringBuilderBig.ToString()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment