Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Module/file-scoped "global" variables?

Reply
Thread Tools

Module/file-scoped "global" variables?

 
 
Pavel Smerk
Guest
Posts: n/a
 
      08-03-2006
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.
 
Reply With Quote
 
 
 
 
Logan Capaldo
Guest
Posts: n/a
 
      08-03-2006

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





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments