From: Peter Kwangjun Suk Date: 2003-01-24T04:36:33+09:00 Subject: Re: emulating C preprocessor's #if On Fri, 24 Jan 2003 03:36:04 +0900 Joel VanderWerf wrote: > Usually, when writing ruby code, I don't worry about conditional > compilation, and just use a run-time test. What about using the Factory pattern, and instantiating an object of a platform-specific subclass? This lets you limit conditional code to as few places as possible. > But for some critical code, I > prefer to factor the conditional out to compile time. The Factory pattern will give you at least the same performance in interpreted Ruby as conditional compilation. Since you have late binding for method calls in Ruby, you are getting the flexibility of conditional compilation "for free." In fact, depending on how many conditional compilations you avoid, and when they happen, it may give you higher performance. (Perhaps I don't have a clear picture of what you propose?) > Unfortunately, > this can require either a lot of duplication of source code, or > awkwardly factoring out sub-methods which are conditionally compiled. Done correctly, it will also avoid both of the above. Like most OO Patterns, this is just a specific case of using Martin Fowler's "Replace Conditional with Polymorphism" refactoring. --Peter Kwangjun Suk