All Posts assertfail

View the Blog on GitHub wallymathieu

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.
  • </ol>

    The finished version is available on github.


  • c#
  • ruby
  • Comments or errors? Fork post and do a pull request on github