[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: #define alternative for Ruby

Daniel Berger

3/11/2007 2:52:00 PM

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

23 Answers

Mike

3/12/2007 12:49:00 AM

0

On Mar 12, 1:52 am, Daniel Berger <djber...@gmail.com> 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.

Tim Hunter

3/12/2007 2:02:00 AM

0

Mike wrote:
> On Mar 12, 1:52 am, Daniel Berger <djber...@gmail.com> 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.

Ara.T.Howard

3/12/2007 5:07:00 AM

0

Ara.T.Howard

3/12/2007 5:21:00 AM

0

Sebastian Hungerecker

3/12/2007 8:16:00 AM

0

ara.t.howard@noaa.gov wrote:
> On Mon, 12 Mar 2007, Mike wrote:
> > 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.
>
> perhaps i'm being dense, but i fail to see how the above example is none
> other than a simple function in ruby:

Imagine you would call the function with rand() as parameter. With #define
rand would be called three times, with def it would be called once and its
value would be used three times.


--
Ist so, weil ist so
Bleibt so, weil war so

Brian Candler

3/12/2007 8:47:00 AM

0

On Mon, Mar 12, 2007 at 05:16:08PM +0900, Sebastian Hungerecker wrote:
> ara.t.howard@noaa.gov wrote:
> > On Mon, 12 Mar 2007, Mike wrote:
> > > 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.
> >
> > perhaps i'm being dense, but i fail to see how the above example is none
> > other than a simple function in ruby:
>
> Imagine you would call the function with rand() as parameter. With #define
> rand would be called three times, with def it would be called once and its
> value would be used three times.

???

irb(main):001:0> def myfunc(x); Math.sin(x)*Math.cos(x)*Math.exp(x); end
=> nil
irb(main):002:0> p myfunc(rand)
0.44720293351854
=> nil
irb(main):003:0> p myfunc(rand)
1.07121692170638
=> nil
irb(main):004:0> p myfunc(rand)
1.01827292445777
=> nil

Now, if you *store* the value of rand within the function, that's a
different case. But Ruby has different idioms for deferred execution: one of
those is

class Foo
def initialize(&blk)
@gen = blk
end
def value
@gen.call
end
end

a = Foo.new { rand }
puts a.value
puts a.value
puts a.value

Brian Candler

3/12/2007 8:53:00 AM

0

On Mon, Mar 12, 2007 at 12:56:26PM +0900, Michael Strelnikov wrote:
> >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.

Do you really need to abstract out "var ||= val" into its own function?

If for some reason you do, then local variables are not the right
abstraction. Local variables are statically created when code is parsed (*);
local variables are not objects; and you cannot take references to them.

Use instance variables on an object instead.

Regards,

Brian.

(*) which means you can create them using "eval", but I'm pretty sure you
don't want to do that.

Mike

3/12/2007 8:58:00 AM

0

On Mar 12, 4:07 pm, ara.t.how...@noaa.gov wrote:
> On Mon, 12 Mar 2007, Michael Strelnikov wrote:
> >> 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.
>
> harp:~ > cat a.rb
> unless defined? var
> var = 42
> end
>
> p var
>
> harp:~ > ruby a.rb
> 42
>
> -a
> --
> be kind whenever possible... it is always possible.
> - the dalai lama

I know it but imagine you must do it 100 times?
I'd rather write:
set_default(var1, 15)
set_default(myvar1, "string")
....
set_default(lastvar, myhash)

In your case I must use "var" twice. Don't you think it is too much?

Mike

3/12/2007 9:00:00 AM

0

On Mar 12, 4:21 pm, ara.t.how...@noaa.gov wrote:
> On Mon, 12 Mar 2007, Mike wrote:
>
> > 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.
>
> perhaps i'm being dense, but i fail to see how the above example is none other
> than a simple function in ruby:
>
> harp:~ > cat a.rb
> def myfunc x
> Math.sin(x) * Math.cos(x) * Math.exp(x)
> end
>
> # x as function
> def x() 42 end
> p myfunc(x)
>
> # x as constant
> X = 42
> p myfunc(X)
>
> # x as variable
> x = 42
> p myfunc(x)
>
> harp:~ > ruby a.rb
> 6.37609775534436e+17
> 6.37609775534436e+17
> 6.37609775534436e+17
>
> text macros like the above are really only useful in static/strong typed
> languages. in ruby, they are rather meaningless.
>
> regards.
>
> -a
> --
> be kind whenever possible... it is always possible.
> - the dalai lama

Just try to implement:
set_default(var, val)
-- if variable "var" is not defined (not existed) then assign value
"val"

Mike

3/12/2007 9:01:00 AM

0

On Mar 12, 7:16 pm, Sebastian Hungerecker <sep...@googlemail.com>
wrote:
> ara.t.how...@noaa.gov wrote:
> > On Mon, 12 Mar 2007, Mike wrote:
> > > 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.
>
> > perhaps i'm being dense, but i fail to see how the above example is none
> > other than a simple function in ruby:
>
> Imagine you would call the function with rand() as parameter. With #define
> rand would be called three times, with def it would be called once and its
> value would be used three times.
>
> --
> Ist so, weil ist so
> Bleibt so, weil war so

Just try to implement:
set_default(var, val)
-- if variable "var" is not defined (not existed) then assign value
"val"