Writing rubyish c# or porting ruby code to c#
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
Comments or errors?
Fork this post and do a pull request on github