File scope variable

  • Thread starter Martin Martinos
  • Start date
M

Martin Martinos

I tried to find a way to have a variable that has a file scope. A
variable that I can access in each function of each class but which is
private to my .rb file.

Does anybody has a idea?
 
7

7stud --

Martin said:
A
variable that I can access in each function of each class but which is
private to my .rb file.

Does anybody has a idea?

Yes. Don't do it. That's a horrible idea. If a method in your class
needs a value, pass the value as an argument.
 
M

Martin Martinos

7stud said:
Yes. Don't do it. That's a horrible idea. If a method in your class
needs a value, pass the value as an argument.


I agree with you on this point, but in some circumstance it would be
interesting to have file scoped variables. There are at least one
built-in variable that is local to the current file and it's the
__FILE__ variable. In fact in my require function I would use a local
variable that tells me what is the current file folder path. I would
also use it to localize the path of the binary files that my script
uses. I don't think in that case its a bad idea since __FILE__ has also
a file scope.

example:

require 'pathname'
my_path = Pathname.new(__FILE__).realpath.dirname

require my_path +'mylib'

def exec_my_bin()
`#{my_path + 'bin/mybin'}`
end
 
R

Rick DeNatale

There are at least one
built-in variable that is local to the current file and it's the
__FILE__ variable. In fact in my require function I would use a local
variable that tells me what is the current file folder path. I would
also use it to localize the path of the binary files that my script
uses. I don't think in that case its a bad idea since __FILE__ has also
a file scope.

Well, it's not that __FILE__ has a file scope, it's that it is a
pseudovariable which is magically set to the current file name.

If you really need to do this, maybe a hash keyed by __FILE__ sort of
like a file-local as an analogy to a thread local variable.
 
R

Robert Klemme

2007/10/3 said:
I agree with you on this point, but in some circumstance it would be
interesting to have file scoped variables. There are at least one
built-in variable that is local to the current file and it's the
__FILE__ variable. In fact in my require function I would use a local
variable that tells me what is the current file folder path. I would
also use it to localize the path of the binary files that my script
uses. I don't think in that case its a bad idea since __FILE__ has also
a file scope.

example:

require 'pathname'
my_path = Pathname.new(__FILE__).realpath.dirname

require my_path +'mylib'

def exec_my_bin()
`#{my_path + 'bin/mybin'}`
end

What is wrong with this solution? my_path *is* visible in the current
file only as far as I can see. Btw, what are you trying to achieve?

Kind regards

robert
 
M

Martin Martinos

Robert said:
What is wrong with this solution? my_path *is* visible in the current
file only as far as I can see. Btw, what are you trying to achieve?

Kind regards

robert

In fact, it only visible in the current file but it's not visible in the
exec_my_bin() function and I don't want to write:

def exec_my_bin()
`#{Pathname.new(__FILE__).realpath.dirname + 'bin/mybin'}`
end

Also I don't want to use a global variable, which I think is a bad idea
since I don't want to use it in another file.

Thanks
 
R

Robert Klemme

2007/10/4 said:
In fact, it only visible in the current file but it's not visible in the
exec_my_bin() function and I don't want to write:

Right you are. I overlooked that one. You could work around that
using a lambda:

$ ruby <<XXX
foo = 100
bar = lambda { puts foo }
bar[]
XXX 100

def exec_my_bin()
`#{Pathname.new(__FILE__).realpath.dirname + 'bin/mybin'}`
end

Also I don't want to use a global variable, which I think is a bad idea
since I don't want to use it in another file.

Reasonable. But still, what do you need that for?

Kind regards

robert
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,268
Messages
2,571,344
Members
48,019
Latest member
Migration_Expert

Latest Threads

Top