From: Michael Strelnikov Date: 2007-03-12T12:56:26+09:00 Subject: Re: #define alternative for Ruby ------=_Part_11500_30674644.1173671783260 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 3/12/07, Timothy Hunter wrote: > > 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. > > Easy. Let's we have following function: * set_default(var, val) -- if variable "var" is not defined (not existed) then assign value "val" else do nothing. The variable could be ANY type including "Fixnum". Try to implement it using standard techniques. ------=_Part_11500_30674644.1173671783260--