From: Timothy Hunter Date: 2007-03-12T11:02:21+09:00 Subject: Re: #define alternative for Ruby Mike wrote: > On Mar 12, 1:52 am, Daniel Berger wrote: > >> Michael Strelnikov wrote: >> >>> Hello all, >>> >>> Does Ruby have something like "#define" for C/C++? Especially with >>> arguments like "#define foo(x) (something_done_with_x(x))". >>> >>> Please don't say that "def" do the same. It doesn't. >>> >> The closest analogue is to conditionally define a constant, then used >> defined? on that constant. >> >> For example, I do something like this in the windows-pr project, where >> some methods are not supported by Win2k or earlier: >> >> module Windows >> module Console >> begin >> AttachConsole = Win32API.new('kernel32','AttachConsole','L', 'I') >> rescue LoadError >> # Requires Windows XP or later >> end >> end >> end >> >> And then, while using the above module: >> >> class MyConsoleClass >> include Windows::Console >> >> def test >> if defined? AttachConsole >> # do something >> else >> # do something else >> end >> end >> end >> >> Regards, >> >> Dan >> > > I was talking more about macros rather then constants. > But I agree that constant are convenient way to substitute "#define > myconst 15". But can you substitute "#define myfunc(x) > (sin(x)*cos(x)*exp(x))"? > You should know that "x" could be as much other functions as a > constant/variable. > I would be interested in hearing what you want to do with text substitution that you cannot already do with standard Ruby metaprogramming techniques.