Check for false

If you want to check for false and alert if not, then no there isn’t. If you use if(val), then anything that evaluates to ‘truthy’, like a non-empty string, will also pass. So it depends on how stringent your criterion is. Using === and !== is generally considered good practice, to avoid accidentally matching truthy … Read more

Benefits of ternary operator vs. if statement

Performance The ternary operator shouldn’t differ in performance from a well-written equivalent if/else statement… they may well resolve to the same representation in the Abstract Syntax Tree, undergo the same optimisations etc.. Things you can only do with ? : If you’re initialising a constant or reference, or working out which value to use inside … Read more

Automapper: complex if else statement in ForMember

In recent versions of AutoMapper, ResolveUsing was removed. Instead, use a new overload of MapFrom: void MapFrom<TResult>(Func<TSource, TDestination, TResult> mappingFunction); Just adding another lambda/function parameter will dispatch to this new overload: CreateMap<TSource, TDest>() .ForMember(dest => dest.SomeDestProp, opt => opt.MapFrom((src, dest) => { TSomeDestProp destinationValue; // mapping logic goes here return destinationValue; }));

Check if file contains string

I would use: if File.readlines(“testfile.txt”).grep(/monitor/).any? or if File.readlines(“testfile.txt”).any?{ |l| l[‘monitor’] } Using readlines has scalability issues though as it reads the entire file into an array. Instead, using foreach will accomplish the same thing without the scalability problem: if File.foreach(“testfile.txt”).grep(/monitor/).any? or if File.foreach(“testfile.txt”).any?{ |l| l[‘monitor’] } See “Why is “slurping” a file not a good … Read more