[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Static variables in Ruby.

Marcin Tyman

7/24/2007 8:41:00 AM

Hi everybody,
Is any way to define static variables over function? As class static
variables there is possible to define a static class variable as
@@name_of_var. But I'm not sure if Ruby let define such variables in
functions (like in C/C++)

Thanks in advance,
MT
--
Posted via http://www.ruby-....

27 Answers

Robert Dober

7/24/2007 9:03:00 AM

0

On 7/24/07, Marcin Tyman <m.tyman@interia.pl> wrote:
> Hi everybody,
> Is any way to define static variables over function? As class static
> variables there is possible to define a static class variable as
> @@name_of_var. But I'm not sure if Ruby let define such variables in
> functions (like in C/C++)
>
> Thanks in advance,
> MT
> --
> Posted via http://www.ruby-....
>
There are three kinds of variables in Ruby
local
@@class_var
@instance_var

My tip, do not use @@class_var, use @inst_var on the correct level

Here goes the classic example of a "static" (forget this expression;)
variable counting instances.
I will use an instance variable of the class (commonly called class
instance variables) for that

549/49 > cat class-inst.rb && ruby class-inst.rb

class A
@count = 0
class << self
attr_accessor :count
end
def initialize val
@a = val
self.class.count += 1
end
end

a=A.new 42
b=A.new 43

p a
p b
p A.count
#<A:0xb7db6764 @a=42>
#<A:0xb7db66b0 @a=43>
2
HTH
Robert

--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

Robert Klemme

7/24/2007 9:04:00 AM

0

2007/7/24, Marcin Tyman <m.tyman@interia.pl>:
> Hi everybody,
> Is any way to define static variables over function? As class static
> variables there is possible to define a static class variable as
> @@name_of_var. But I'm not sure if Ruby let define such variables in
> functions (like in C/C++)

If you mean static variables that are visible in a single method only,
then no, there is no way to do it. You should probably rethink your
design if it relies on such archaic features of other programming
languages. :-)

Kind regards

robert

Jeremy Henty

7/24/2007 9:36:00 AM

0

On 2007-07-24, Robert Klemme <shortcutter@googlemail.com> wrote:

> If you mean static variables that are visible in a single method
> only, then no, there is no way to do it.

*cough*

class Foo
count = 0
define_method(:foo) { count += 1 }
end
x = Foo.new
3.times { puts x.foo }

==>
1
2
3

Regards,

Jeremy Henty

dblack

7/24/2007 9:51:00 AM

0

Jeremy Henty

7/24/2007 11:02:00 AM

0

On 2007-07-24, dblack@wobblini.net <dblack@wobblini.net> wrote:
>
> On Tue, 24 Jul 2007, Jeremy Henty wrote:
>
>> On 2007-07-24, Robert Klemme <shortcutter@googlemail.com> wrote:
>>
>>> If you mean static variables that are visible in a single method
>>> only, then no, there is no way to do it.
>>
>> class Foo
>> count = 0
>> define_method(:foo) { count += 1 }
>> end
>
> "Static" is a misleading term, though. It's just a local variable
> that happens to get wrapped in a closure.

True, but it is still "visible in a single method only", which AIUI
Robert Klemme claimed was undoable. Even though Ruby doesn't have
truely static variables, it can create something that behaves very
like (a common use case of) them. That's all I'm claiming.

Regards,

Jeremy Henty

Robert Dober

7/24/2007 12:06:00 PM

0

On 7/24/07, Jeremy Henty <onepoint@starurchin.org> wrote:
> On 2007-07-24, Robert Klemme <shortcutter@googlemail.com> wrote:
>
> > If you mean static variables that are visible in a single method
> > only, then no, there is no way to do it.
>
> *cough*
>
> class Foo
> count = 0
> define_method(:foo) { count += 1 }
> end
> x = Foo.new
> 3.times { puts x.foo }
>
> ==>
> 1
> 2
> 3
>
> Regards,
>
> Jeremy Henty
>
>

closure based properties, what a nice idea ;)
Terribly slow BTW :(

Robert
--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

Bertram Scharpf

7/24/2007 12:32:00 PM

0

Hi,

Am Dienstag, 24. Jul 2007, 18:03:15 +0900 schrieb Robert Dober:
> On 7/24/07, Marcin Tyman <m.tyman@interia.pl> wrote:
> >Is any way to define static variables over function? As class static
> >variables there is possible to define a static class variable as
> >@@name_of_var. But I'm not sure if Ruby let define such variables in
> >functions (like in C/C++)
> There are three kinds of variables in Ruby
> local
> @@class_var
> @instance_var

Further

$global

which I use almost never (except $1, $> etc.).

> My tip, do not use @@class_var, use @inst_var on the correct level
>
> class A
> @count = 0
> class << self
> attr_accessor :count
> end
> end

So do I.

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

Robert Dober

7/24/2007 12:38:00 PM

0

On 7/24/07, Bertram Scharpf <lists@bertram-scharpf.de> wrote:
> Hi,
>
> Am Dienstag, 24. Jul 2007, 18:03:15 +0900 schrieb Robert Dober:
> > On 7/24/07, Marcin Tyman <m.tyman@interia.pl> wrote:
> > >Is any way to define static variables over function? As class static
> > >variables there is possible to define a static class variable as
> > >@@name_of_var. But I'm not sure if Ruby let define such variables in
> > >functions (like in C/C++)
> > There are three kinds of variables in Ruby
> > local
> > @@class_var
> > @instance_var
>
> Further
>
> $global
LOL completely forgot them, thx.
>
> which I use almost never (except $1, $> etc.).
exactly
<snip>
Robert

--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

Jeremy Henty

7/24/2007 12:50:00 PM

0

On 2007-07-24, Robert Dober <robert.dober@gmail.com> wrote:
> On 7/24/07, Jeremy Henty <onepoint@starurchin.org> wrote:
>>
>> class Foo
>> count = 0
>> define_method(:foo) { count += 1 }
>> end
>
> Terribly slow BTW :(

Why is that? Are all methods created by define_method slow? Or is it
something particular to the way I'm using it here? I'm really getting
into metaprogramming with define_method so I'd like to learn about the
pitfalls.

Regards,

Jeremy Henty

Todd Benson

7/24/2007 1:15:00 PM

0

On 7/24/07, Jeremy Henty <onepoint@starurchin.org> wrote:
> On 2007-07-24, Robert Dober <robert.dober@gmail.com> wrote:
> > On 7/24/07, Jeremy Henty <onepoint@starurchin.org> wrote:
> >>
> >> class Foo
> >> count = 0
> >> define_method(:foo) { count += 1 }
> >> end
> >
> > Terribly slow BTW :(
>
> Why is that? Are all methods created by define_method slow? Or is it
> something particular to the way I'm using it here? I'm really getting
> into metaprogramming with define_method so I'd like to learn about the
> pitfalls.
>
> Regards,
>
> Jeremy Henty

I believe Robert developed a library for using closure based
properties a short while back. Something to do with a breed of dog or
something :) Cool stuff. But he found it to be slow if I remember
correctly.

Todd