File scope variable

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?

Martin M. wrote:

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.

7stud – wrote:

Martin M. wrote:

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.

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

2007/10/3, Martin M. [email protected]:

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

Robert K. wrote:

2007/10/3, Martin M. [email protected]:

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

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

On 10/3/07, Martin M. [email protected] wrote:

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.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

2007/10/4, Martin M. [email protected]:

def exec_my_bin()
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