Module/file-scoped "global" variables?

Discussion in 'Ruby' started by Pavel Smerk, Aug 3, 2006.

  1. Pavel Smerk

    Pavel Smerk Guest

    Hello,

    I have a file with some functions which share some common variables. I
    want to have these functions visible in whole program (or at least in
    some classes by making a module from the file and mix it into these
    classes), but I'd like to make shared variables remain unvisible to the
    rest of the program. Is it possible in Ruby?

    Thanks,

    P.
     
    Pavel Smerk, Aug 3, 2006
    #1
    1. Advertising

  2. On Aug 3, 2006, at 1:05 AM, Pavel Smerk wrote:

    > Hello,
    >
    > I have a file with some functions which share some common
    > variables. I want to have these functions visible in whole program
    > (or at least in some classes by making a module from the file and
    > mix it into these classes), but I'd like to make shared variables
    > remain unvisible to the rest of the program. Is it possible in Ruby?
    >
    > Thanks,
    >
    > P.
    >


    Yeah, if you really want to do this you can do:


    module A

    shared = 1

    define_method:)uses_shared1) { shared += 1; puts shared }
    define_method:)uses_shared2) { puts shared }
    end



    class B
    include A
    end

    b = B.new
    b.uses_shared1
    b.uses_shared2
     
    Logan Capaldo, Aug 3, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    4
    Views:
    8,013
    mike_treseler
    Nov 9, 2004
  2. Andy Fish

    block scoped variables

    Andy Fish, Aug 15, 2003, in forum: Java
    Replies:
    3
    Views:
    425
    Chris Uppal
    Aug 16, 2003
  3. Madhav
    Replies:
    4
    Views:
    305
    Kenny McCormack
    Jan 8, 2006
  4. Madhav
    Replies:
    27
    Views:
    568
    Dave Thompson
    Jan 16, 2006
  5. Replies:
    3
    Views:
    114
Loading...

Share This Page