Professional Web Applications Themes

C's static equivalent - Ruby

Hi! Is there something similar to C's static keyword? I want a single function, which has to access and modify a permanent variable. Any solution without bundling the function to a class? Gergo -- +-[ Kontra, Gergely<kgergelymcl.hu> PhD student Room IB113 ]---------+ | [url]http://www.mcl.hu/~kgergely[/url] "Olyan langesz vagyok, hogy | | Mobil:(+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom" | +-- Magyar php mirror es magyar php dokumentacio: [url]http://hu.php.net[/url] --+...

  1. #1

    Default C's static equivalent

    Hi!

    Is there something similar to C's static keyword?
    I want a single function, which has to access and modify a permanent
    variable. Any solution without bundling the function to a class?

    Gergo
    --
    +-[ Kontra, Gergely<kgergelymcl.hu> PhD student Room IB113 ]---------+
    | [url]http://www.mcl.hu/~kgergely[/url] "Olyan langesz vagyok, hogy |
    | Mobil:(+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom" |
    +-- Magyar php mirror es magyar php dokumentacio: [url]http://hu.php.net[/url] --+

    KONTRA Gergely Guest

  2. #2

    Default Re: C's static equivalent

    >>>>> "K" == KONTRA Gergely <kgergelymlabdial.hit.bme.hu> writes:

    K> Is there something similar to C's static keyword?
    K> I want a single function, which has to access and modify a permanent
    K> variable. Any solution without bundling the function to a class?

    Use a closure

    Guy Decoux

    ts Guest

  3. #3

    Default Re: C's static equivalent

    On 0807, ts wrote:
    > >>>>> "K" == KONTRA Gergely <kgergelymlabdial.hit.bme.hu> writes:
    >
    > K> Is there something similar to C's static keyword?
    > K> I want a single function, which has to access and modify a permanent
    > K> variable. Any solution without bundling the function to a class?
    > Use a closure
    Could you be more verbose? Eg. how to print out how many times a
    function was called.

    Gergo
    --
    +-[ Kontra, Gergely<kgergelymcl.hu> PhD student Room IB113 ]---------+
    | [url]http://www.mcl.hu/~kgergely[/url] "Olyan langesz vagyok, hogy |
    | Mobil:(+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom" |
    +-- Magyar php mirror es magyar php dokumentacio: [url]http://hu.php.net[/url] --+

    KONTRA Gergely Guest

  4. #4

    Default Re: C's static equivalent

    >>>>> "K" == KONTRA Gergely <kgergelymlabdial.hit.bme.hu> writes:
    >> Use a closure
    K> Could you be more verbose? Eg. how to print out how many times a

    in ruby, yes. in english, no :-)

    K> function was called.

    svg% cat b.rb
    #!/usr/bin/ruby
    def test1
    a = 10
    proc { a += 1; puts a }
    end

    class Object
    define_method(:tt, test1)
    end

    tt
    tt

    uu = test1
    uu[]
    uu[]
    svg%

    svg% b.rb
    11
    12
    11
    12
    svg%



    Guy Decoux

    ts Guest

  5. #5

    Default Re: C's static equivalent

    More or less:

    def make_closure
    perm_variable = 0
    return proc {
    perm_variable += 1
    puts "I have been called ", perm_variable, " times."
    }
    end

    def use_closure
    fun = make_closure
    fun.call
    fun.call
    fun.call
    end

    use_closure

    Should print out

    I have been called 1 times.
    I have been called 2 times.
    I have been called 3 times.

    However, each time you call make_closure, it returns a separate context, so:

    def use_closure2
    fun1 = make_closure
    fun2 = make_closure

    fun1.call
    fun2.call
    fun1.call
    end

    use_closure2

    Should print:

    I have been called 1 times.
    I have been called 1 times.
    I have been called 2 times.

    Because fun1 and fun2 store two separate states of perm_variable.
    At least that's my understanding of closures.

    As far as I know, there's nothing quite like C's static
    in Ruby, since there's no way to make a variable local
    in scope to a function, but persistant in state over all
    calls to that function (at least that I know of).

    - Dan

    ----- Original Message -----
    From: KONTRA Gergely <kgergelymlabdial.hit.bme.hu>
    Date: Thursday, August 7, 2003 9:59 am
    Subject: Re: C's static equivalent
    > On 0807, ts wrote:
    > > >>>>> "K" == KONTRA Gergely <kgergelymlabdial.hit.bme.hu> writes:
    > >
    > > K> Is there something similar to C's static keyword?
    > > K> I want a single function, which has to access and modify a
    > permanent> K> variable. Any solution without bundling the function
    > to a class?
    > > Use a closure
    > Could you be more verbose? Eg. how to print out how many times a
    > function was called.
    >
    > Gergo
    > --
    > +-[ Kontra, Gergely<kgergelymcl.hu> PhD student Room IB113 ]------
    > ---+
    > | [url]http://www.mcl.hu/~kgergely[/url] "Olyan langesz vagyok,
    > hogy |
    > | Mobil:(+36 20) 356 9656 ICQ: 175564914 poroltoval kellene
    > jarnom" |
    > +-- Magyar php mirror es magyar php dokumentacio:
    > [url]http://hu.php.net[/url] --+
    >
    >

    djd15@cwru.edu Guest

  6. #6

    Default Re: C's static equivalent

    On Thu, Aug 07, 2003 at 11:18:17PM +0900, [email]djd15cwru.edu[/email] wrote:
    > As far as I know, there's nothing quite like C's static
    > in Ruby, since there's no way to make a variable local
    > in scope to a function, but persistant in state over all
    > calls to that function (at least that I know of).
    A C 'static' variable is just a global variable with a lexically local
    scope, so a class variable (count += 1) is probably the closest
    equivalent. The scope extends to the whole object though, not just the
    method.

    Regards,

    Brian.

    Brian Candler Guest

  7. #7

    Default Re: C's static equivalent

    ----- Original Message -----
    From: "ts" <decouxmoulon.inra.fr>
    To: "ruby-talk ML" <ruby-talkruby-lang.org>
    Cc: <ruby-talkruby-lang.org>
    Sent: Thursday, August 07, 2003 9:06 AM
    Subject: Re: C's static equivalent

    > >>>>> "K" == KONTRA Gergely <kgergelymlabdial.hit.bme.hu> writes:
    >
    > >> Use a closure
    > K> Could you be more verbose? Eg. how to print out how many times a
    >
    > in ruby, yes. in english, no :-)
    It would be worthwhile for all of us to
    learn French in order to learn more of
    your wisdom. :)

    Hal

    --
    Hal Fulton
    [email]hal9000hypermetrics.com[/email]



    Hal E. Fulton Guest

  8. #8

    Default Re: C's static equivalent

    Saluton!

    * Hal E. Fulton; 2003-08-07, 17:05 UTC:
    > It would be worthwhile for all of us to learn French in order to
    > learn more of your wisdom. :)
    Si on ne doit pas répondre en français :->
    As long as we are not required to answer in french ...

    Gis,

    Josef 'Jupp' Schugt
    --
    N'attribuez jamais à la malice ce que l'incompétence explique !
    -- Napoléon

    Josef 'Jupp' Schugt Guest

Similar Threads

  1. Replies: 2
    Last Post: December 3rd, 12:47 AM
  2. #39664 [NEW]: Invokation of non-static method as static issue
    By john dot hargrove at sellingsource dot com in forum PHP Bugs
    Replies: 3
    Last Post: November 28th, 10:33 PM
  3. Replies: 3
    Last Post: October 5th, 03:04 PM
  4. Replies: 1
    Last Post: September 11th, 09:21 PM
  5. ACT! Equivalent for Mac?
    By compuser in forum Mac Applications & Software
    Replies: 2
    Last Post: July 10th, 05:34 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139