Static variables in Ruby

Hi --

>> Nobuyoshi Nakada wrote:
>> > Hi,
>> >
>> > At Tue, 24 Jul 2007 17:40:57 +0900,
>> > Marcin Tyman wrote in [ruby-talk:261483]:
>> >> 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++)
>> >
>> > def foo(x)
>> > (0..0).instance_eval{x,@x = @x,x}
>> > x
>> > end
>> > 3.times{|i|p foo(i)} #=> nil, 1, 2
>> >
>> > The literal object can be Float, Regexp or Bignum, but Range
>> > would be faster a bit.
>> >
>>
>> Interesting. That is because Float, Regexp, and Bignum are instantiated
>> per scope, right?
>>
>> Sometimes a little abstraction leakage can be useful!
>>
>> --
>> vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
>>
> I did not believe this,and why should I?
> But it seems to be true :wink:
> def foo(x)
> (0..0).instance_eval{x,@x = @x,x}
> x
> end
>
> 3.times{ |i| puts foo(i) }
> (0..0).instance_eval{@x=42}
> 3.times{ |i| puts foo(i) }
>
> Run this for a pleasant surprise :slight_smile:

I get the same result with or without the line between the two times
calls. Are you seeing it output 42?

No that is the pleasant surprise :slight_smile:
If I understood the Guru Sans correctly that is because the
interpreter defines (0..0) scope locally -- the same object_id
notwithstanding -- amazing feature indeed.
Needless to say that one should rather not use it :wink:

Cheers
Robert

···

On 7/25/07, dblack@wobblini.net <dblack@wobblini.net> wrote:

On Wed, 25 Jul 2007, Robert Dober wrote:
> On 7/24/07, Joel VanderWerf <vjoel@path.berkeley.edu> wrote:

David

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

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

> >>
> >> class Foo
> >> count = 0
> >> define_method(:foo) { count += 1 }
> >> end
> >
> > Terribly slow BTW :frowning:
>
> 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 :slight_smile: Cool stuff. But he found it to be slow if I remember
correctly.

Thx, properties are broken, but thx anyway :slight_smile:

···

On 7/24/07, Todd Benson <caduceass@gmail.com> wrote:

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:

Todd

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

Fair enough. In fact that often happens to me; I implement something
in a clever way at first, then when it's time to clean up and refactor
I see that I was being *too* clever and rewrite it much more
straightforwardly. I guess that means I'm clever, but meta-stupid
(for being too clever), but meta-meta-clever (for noticing when I'm
being meta-stupid). I think I'll quit there while I'm ahead! :slight_smile:

Regards,

Jeremy Henty

···

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

... if you have a design complex enough make separation of your
instance state by method necessary then you should break up
functionality into more classes to keep it manageable.