Writing rubyish C# or porting ruby code to C#
27 Oct 2013
Since I liked the ruby version for parsing phone numbers, I ported it to C#. In order to avoid having to rewrite to much I did the following:
- To begin with, the entire project was using ruby name convention. This simplifies things.
- Whenever there is a missing method where the behavior of the method is not the same as the C# one, I added an extension method. For instance I implemented a variant of gsub. The behavior of this gsub uses $1 for matching groups (since it’s more natural to remove a bit of code than make a full port).
- I tried to translate different ruby constructs into C#. For instance the lazy pattern found in the library
@variable ||= init_variable I had to replace with
_variable ?? (_variable=InitVariable())
- After all the tests became green and I had checked in the initial version, it was quite simple to change name convention using resharper.
The finished version is available on github
Do you want to send a comment or give me a hint about any issues with a blog post: Open up an issue on GitHub.
Do you want to fix an error or add a comment published on the blog? You can do a
fork of this post and do a pull request on github.