From: Yuri Leikind Date: 2003-01-24T03:51:44+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. But for some critical code, > I prefer to factor the conditional out to compile time. Unfortunately, > > this can require either a lot of duplication of source code, or > awkwardly factoring out sub-methods which are conditionally compiled. > Sometimes, the following approach is more convenient: > > module M > solaris = (RUBY_PLATFORM =~ /solaris/i) > linux = (RUBY_PLATFORM =~ /linux/i) > windows = (RUBY_PLATFORM =~ /win/i) > > module_eval %{ > def self.print_platform > print "Platform is... " > #{ > if solaris then %{ > puts "solaris" > } > elsif linux then %{ > puts "linux" > } > elsif windows then %{ > puts "windows" > } > else %{ > puts "unknown" > } > end > } > end > } > end > > M.print_platform Perhaps I might have misunderstood the main point of your letter, but I think Ruby is dynamic enough to do what you want without any other changes in the core language: module M def M.print_platform print "Platform is..." + M.platform_dependent_code() + "\n" end case RUBY_PLATFORM when /solaris/i then def M.platform_dependent_code "Solaris" end when /linux/i then def M.platform_dependent_code "Solaris" end when /win/i then def M.platform_dependent_code "Solaris" end else def M.platform_dependent_code "unknown" end end end M.print_platform ___ Best regards, Yuri Leikind The debate rages on: Is PL/I Bachtrian or Dromedary?