Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Static variables in Ruby.
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  23 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Marcin Tyman  
View profile  
 More options Jul 24 2007, 4:40 am
Newsgroups: comp.lang.ruby
From: Marcin Tyman <m.ty...@interia.pl>
Date: Tue, 24 Jul 2007 17:40:57 +0900
Local: Tues, Jul 24 2007 4:40 am
Subject: Static variables in Ruby.
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-forum.com/.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Dober  
View profile  
 More options Jul 24 2007, 5:03 am
From: "Robert Dober" <robert.do...@gmail.com>
Date: Tue, 24 Jul 2007 18:03:15 +0900
Local: Tues, Jul 24 2007 5:03 am
Subject: Re: Static variables in Ruby.
On 7/24/07, Marcin Tyman <m.ty...@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-forum.com/.

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Klemme  
View profile  
 More options Jul 24 2007, 5:04 am
From: "Robert Klemme" <shortcut...@googlemail.com>
Date: Tue, 24 Jul 2007 18:04:13 +0900
Local: Tues, Jul 24 2007 5:04 am
Subject: Re: Static variables in Ruby.
2007/7/24, Marcin Tyman <m.ty...@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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeremy Henty  
View profile  
 More options Jul 24 2007, 5:40 am
Newsgroups: comp.lang.ruby
From: Jeremy Henty <onepo...@starurchin.org>
Date: Tue, 24 Jul 2007 18:40:19 +0900
Local: Tues, Jul 24 2007 5:40 am
Subject: Re: Static variables in Ruby.
On 2007-07-24, Robert Klemme <shortcut...@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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
dbl...@wobblini.net  
View profile  
 More options Jul 24 2007, 5:50 am
From: dbl...@wobblini.net
Date: Tue, 24 Jul 2007 18:50:39 +0900
Local: Tues, Jul 24 2007 5:50 am
Subject: Re: Static variables in Ruby.
Hi --

"Static" is a misleading term, though.  It's just a local variable
that happens to get wrapped in a closure.  In general, I don't think
Ruby and the word "static" mix very well :-)  Constants are sort of
statically scoped, but that's about it.

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)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeremy Henty  
View profile  
 More options Jul 24 2007, 7:04 am
Newsgroups: comp.lang.ruby
From: Jeremy Henty <onepo...@starurchin.org>
Date: Tue, 24 Jul 2007 20:04:58 +0900
Local: Tues, Jul 24 2007 7:04 am
Subject: Re: Static variables in Ruby.
On 2007-07-24, dbl...@wobblini.net <dbl...@wobblini.net> wrote:

> On Tue, 24 Jul 2007, Jeremy Henty wrote:

>> On 2007-07-24, Robert Klemme <shortcut...@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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Dober  
View profile  
 More options Jul 24 2007, 8:06 am
From: "Robert Dober" <robert.do...@gmail.com>
Date: Tue, 24 Jul 2007 21:06:02 +0900
Local: Tues, Jul 24 2007 8:06 am
Subject: Re: Static variables in Ruby.
On 7/24/07, Jeremy Henty <onepo...@starurchin.org> wrote:

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bertram Scharpf  
View profile  
 More options Jul 24 2007, 8:31 am
From: Bertram Scharpf <li...@bertram-scharpf.de>
Date: Tue, 24 Jul 2007 21:31:33 +0900
Local: Tues, Jul 24 2007 8:31 am
Subject: Re: Static variables in Ruby.
Hi,

Am Dienstag, 24. Jul 2007, 18:03:15 +0900 schrieb Robert Dober:

> On 7/24/07, Marcin Tyman <m.ty...@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-scharpf.de


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Dober  
View profile  
 More options Jul 24 2007, 8:37 am
From: "Robert Dober" <robert.do...@gmail.com>
Date: Tue, 24 Jul 2007 21:37:52 +0900
Local: Tues, Jul 24 2007 8:37 am
Subject: Re: Static variables in Ruby.
On 7/24/07, Bertram Scharpf <li...@bertram-scharpf.de> wrote:

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeremy Henty  
View profile  
 More options Jul 24 2007, 8:55 am
Newsgroups: comp.lang.ruby
From: Jeremy Henty <onepo...@starurchin.org>
Date: Tue, 24 Jul 2007 21:55:02 +0900
Local: Tues, Jul 24 2007 8:55 am
Subject: Re: Static variables in Ruby.
On 2007-07-24, Robert Dober <robert.do...@gmail.com> wrote:

> On 7/24/07, Jeremy Henty <onepo...@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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Todd Benson  
View profile  
 More options Jul 24 2007, 9:15 am
From: "Todd Benson" <caduce...@gmail.com>
Date: Tue, 24 Jul 2007 22:15:15 +0900
Local: Tues, Jul 24 2007 9:15 am
Subject: Re: Static variables in Ruby.
On 7/24/07, Jeremy Henty <onepo...@starurchin.org> wrote:

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Dober  
View profile  
 More options Jul 24 2007, 9:16 am
From: "Robert Dober" <robert.do...@gmail.com>
Date: Tue, 24 Jul 2007 22:16:15 +0900
Local: Tues, Jul 24 2007 9:16 am
Subject: Re: Static variables in Ruby.
On 7/24/07, Jeremy Henty <onepo...@starurchin.org> wrote:

No it is the lookup, if you are interested I can send you my
benchmarks, without any twists it was 4 to 5 times slower than ivar
lookup and with twists like read_only, write_once, rw_locks it quickly
climbed to a factor worse than 10 :(
Robert

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Dober  
View profile  
 More options Jul 24 2007, 9:22 am
From: "Robert Dober" <robert.do...@gmail.com>
Date: Tue, 24 Jul 2007 22:22:44 +0900
Local: Tues, Jul 24 2007 9:22 am
Subject: Re: Static variables in Ruby.
On 7/24/07, Todd Benson <caduce...@gmail.com> wrote:

Thx, properties are broken, but thx anyway :)

> Todd

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

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Klemme  
View profile  
 More options Jul 24 2007, 9:23 am
From: "Robert Klemme" <shortcut...@googlemail.com>
Date: Tue, 24 Jul 2007 22:23:26 +0900
Local: Tues, Jul 24 2007 9:23 am
Subject: Re: Static variables in Ruby.
2007/7/24, Jeremy Henty <onepo...@starurchin.org>:

Well, in Ruby there is usually a workaround.  But there is no real
built in static method variables feature.  So we're both correct in a
way. :-)

Also, I did not want to encourage people to use this pattern because I
believe it's not good.  Here's why: 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.

Kind regards

robert


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeremy Henty  
View profile  
 More options Jul 24 2007, 2:10 pm
Newsgroups: comp.lang.ruby
From: Jeremy Henty <onepo...@starurchin.org>
Date: Wed, 25 Jul 2007 03:10:02 +0900
Local: Tues, Jul 24 2007 2:10 pm
Subject: Re: Static variables in Ruby.
On 2007-07-24, Robert Klemme <shortcut...@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.

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!  :-)

Regards,

Jeremy Henty


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nobuyoshi Nakada  
View profile  
 More options Jul 24 2007, 3:00 pm
From: Nobuyoshi Nakada <n...@ruby-lang.org>
Date: Wed, 25 Jul 2007 04:00:23 +0900
Local: Tues, Jul 24 2007 3:00 pm
Subject: Re: Static variables in Ruby.
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.

--
Nobu Nakada


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joel VanderWerf  
View profile  
 More options Jul 24 2007, 5:59 pm
From: Joel VanderWerf <vj...@path.berkeley.edu>
Date: Wed, 25 Jul 2007 06:59:46 +0900
Local: Tues, Jul 24 2007 5:59 pm
Subject: Re: Static variables in Ruby.

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nobuyoshi Nakada  
View profile  
 More options Jul 24 2007, 8:50 pm
From: Nobuyoshi Nakada <n...@ruby-lang.org>
Date: Wed, 25 Jul 2007 09:50:12 +0900
Local: Tues, Jul 24 2007 8:50 pm
Subject: Re: Static variables in Ruby.
Hi,

At Wed, 25 Jul 2007 06:59:46 +0900,
Joel VanderWerf wrote in [ruby-talk:261627]:

> Interesting. That is because Float, Regexp, and Bignum are instantiated
> per scope, right?

Yes, per node in accurate.

> Sometimes a little abstraction leakage can be useful!

Note that it works with the current MRI.  I won't be surprised
if it never work with JRuby, Rubinius, IronRuby, etc.

--
Nobu Nakada


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Dober  
View profile  
 More options Jul 25 2007, 4:33 am
From: "Robert Dober" <robert.do...@gmail.com>
Date: Wed, 25 Jul 2007 17:33:43 +0900
Local: Wed, Jul 25 2007 4:33 am
Subject: Re: Static variables in Ruby.
On 7/24/07, Joel VanderWerf <vj...@path.berkeley.edu> wrote:

I did not believe this,and why should I?
But it seems to be true ;)
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 :)

Robert

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Klemme  
View profile  
 More options Jul 25 2007, 4:39 am
From: "Robert Klemme" <shortcut...@googlemail.com>
Date: Wed, 25 Jul 2007 17:39:33 +0900
Local: Wed, Jul 25 2007 4:39 am
Subject: Re: Static variables in Ruby.
2007/7/25, Robert Dober <robert.do...@gmail.com>:

The reason why it works is this:

>> def f; (0..0).object_id end
=> nil
>> 5.times {p f}

69868270
69868270
69868270
69868270
69868270
=> 5

The Ruby interpreter optimizes this when both ends are constant.

>> def g(a)(0..a).object_id end
=> nil
>> 5.times {p g(1)}

69627090
69626930
69626820
69626680
69626560
=> 5

Kind regards

robert


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Dober  
View profile  
 More options Jul 25 2007, 5:17 am
From: "Robert Dober" <robert.do...@gmail.com>
Date: Wed, 25 Jul 2007 18:17:13 +0900
Local: Wed, Jul 25 2007 5:17 am
Subject: Re: Static variables in Ruby.
On 7/25/07, Robert Klemme <shortcut...@googlemail.com> wrote:

Actually this is a reason why it should *not* work
def f; (0..0).object_id end
2.times{ p f }
p (0..0).object_id
Nevertheless it does because of the scope local optimization if I
understood Joël correctly.

Hmm as we are exploring it, let us see if it works for all constants,
somehow I doubt it...
Fixnum ranges work, Bignum ranges do not
646/146 > cat rang_oid.rb && ruby rang_oid.rb
#!/usr/local/bin/ruby
# vim: sw=2 sts=2 ft=ruby expandtab tw=0:

def f; "08%x" % (1..1073741823).object_id end
def b; "08%x" % (1..1073741824).object_id end

puts "f"
3.times{ puts f }
puts "b"
3.times{ puts b }
f
08..fdbeb46ae
08..fdbeb46ae
08..fdbeb46ae
b
08..fdbeb4366
08..fdbeb4334
08..fdbeb4302

So for bignums the values will not be kept.

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
dbl...@wobblini.net  
View profile  
 More options Jul 25 2007, 6:55 am
From: dbl...@wobblini.net
Date: Wed, 25 Jul 2007 19:55:23 +0900
Local: Wed, Jul 25 2007 6:55 am
Subject: Re: Static variables in Ruby.
Hi --

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

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)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Dober  
View profile  
 More options Jul 25 2007, 7:20 am
From: "Robert Dober" <robert.do...@gmail.com>
Date: Wed, 25 Jul 2007 20:20:10 +0900
Local: Wed, Jul 25 2007 7:20 am
Subject: Re: Static variables in Ruby.
On 7/25/07, dbl...@wobblini.net <dbl...@wobblini.net> wrote:

No that is the pleasant surprise :)
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 ;)

Cheers
Robert

> 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

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google