Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > problem accessing variables in module

Reply
Thread Tools

problem accessing variables in module

 
 
Tom Cloyd
Guest
Posts: n/a
 
      05-04-2009
I keep getting blocked trying to do a very simple thing - I want to
"include" - in the sense of causing a mere insertion of text - some
variables and methods, residing in an external file, in my main code. I
just want my main to read a file and insert what's in it into the code
stream and then forget where it came from. Apparently this isn't
possible in any way, sadly.

So, trying to play the game according to the rules, I find that a mere
"require {filename}", then an "include {module name in that file}"
doesn't do it, because the namespaces aren't merged, as I would have
expected. (An include that doesn't include? Huh?)

So, I have learned to use Object::instance_eval - confusing because I
don't have an instance. But never mind that, 'cause it worked. But that
method only allows me to access module methods (i.e., they aren't in a
class). Now I want to access a variable that is set in the module, but
not in a method or anything else. If I make the variable a constant,
everything works, but I can't do that in my application, I need a
variable. When a mere ordinary variable didn't work, I tried making it
an instance variable. No good. Then I tried calling "attr_reader" in the
Module. Nope. Nothing works, not even using Object::instance_variable_get.

I realize a real Ruby programmer would start creating classes everywhere
and then it would all work (I guess). I'm trying very hard to keep it
simple, because the concept is simple. I don't need/want a class. Just
the variable, which is initialized in the module. Simple idea - not
simple to do - or at least I haven't discovered how to do it.

Here's a code outline.

=== file utils.rb ===

module Utils
@menu = [{data}] # <= what I'm trying to access
def self.meth_x
{ code }
end
end

=== end file utils.rb ===

=== main ===

Class SN
require utils.rb
include Utils
def meth_a
# attempt 1 - doesn't work
Utils::@menu.each do |i|
{code}
end
# attempt 2 - doesn't work
x_menu = Utils.instance_variable_get( "menu" )
utils_menu.each do |i|
{code}
end
end
end
=== end main ===

Can someone give me the "missing piece" or suggest what I need to do to
get this working?

Many thanks!

Tom


 
Reply With Quote
 
 
 
 
Tom Cloyd
Guest
Posts: n/a
 
      05-05-2009
Robert Klemme wrote:
> 2009/5/4 Tom Cloyd <(E-Mail Removed)>:
>
>> I keep getting blocked trying to do a very simple thing - I want to
>> "include" - in the sense of causing a mere insertion of text - some
>> variables and methods, residing in an external file, in my main code. I just
>> want my main to read a file and insert what's in it into the code stream and
>> then forget where it came from. Apparently this isn't possible in any way,
>> sadly.
>>

>
> That sounds as if you wanted to do
>
> class SN
> eval(File.read("utils.rb")) # utils.rb contains method definitions etc.
> end
>
>
>> So, trying to play the game according to the rules, I find that a mere
>> "require {filename}", then an "include {module name in that file}" doesn't
>> do it, because the namespaces aren't merged, as I would have expected. (An
>> include that doesn't include? Huh?)
>>

>
>
>> I realize a real Ruby programmer would start creating classes everywhere and
>>

>
> You do have classes already.
>
>
>> then it would all work (I guess). I'm trying very hard to keep it simple,
>> because the concept is simple. I don't need/want a class. Just the
>> variable, which is initialized in the module. Simple idea - not simple to do
>> - or at least I haven't discovered how to do it.
>>

>
>
>> Can someone give me the "missing piece" or suggest what I need to do to get
>> this working?
>>

>
> There's a sample attached which allows do extend individual instances
> as well as include in classes.
>
> Kind regards
>
> robert
>
>
>

Robert, this is exactly what I was looking for. I'm a bit stunned, after
so many failure to even communicate to others what I was trying to do.
It's so straightforward. You got it solved right away. Thanks! This is
very helpful to be, in my modest efforts to make a tool from which I
will get a lot use! Thanks for your time and help.

Tom

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< http://www.velocityreviews.com/forums/(E-Mail Removed) >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~


 
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Accessing Module variables from another Module cjt22@bath.ac.uk Python 3 09-05-2007 01:30 PM
Accessing class member variables - properties or variables? dwok Java 7 03-04-2005 03:54 AM
accessing session variables in a asp.net module subrahmanya raju via .NET 247 ASP .Net 1 06-26-2004 07:19 PM
Problem with module variables / module deallocation . . . alejandro david weil Python 0 05-12-2004 03:26 AM



Advertisments