How to use Active Support core extensions

Since using Rails should handle this automatically I’m going to assume you’re trying to add Active Support to a non-Rails script. Read “How to Load Core Extensions”. Active Support’s methods got broken into smaller groups in Rails 3, so we don’t end up loading a lot of unneeded stuff with a simple require ‘activesupport’. Now … Read more

How do I write an extension method in JavaScript?

JavaScript doesn’t have an exact analogue for C#’s extension methods. JavaScript and C# are quite different languages. The nearest similar thing is to modify the prototype object of all string objects: String.prototype. In general, best practice is not to modify the prototypes of built-in objects in library code meant to be combined with other code … Read more

Convert string to nullable type (int, double, etc…)

Another thing to keep in mind is that the string itself might be null. public static Nullable<T> ToNullable<T>(this string s) where T: struct { Nullable<T> result = new Nullable<T>(); try { if (!string.IsNullOrEmpty(s) && s.Trim().Length > 0) { TypeConverter conv = TypeDescriptor.GetConverter(typeof(T)); result = (T)conv.ConvertFrom(s); } } catch { } return result; }