(noob question) use a variable from an external .rb file

Hello,

This is my question:

I need to execute the same piece of code in several different .rb files.

I don’t want to write it over and over again, so I choose to put that
piece of code in an external .rb file and invoke it in one of my files
like this:

require ‘shared_classes.rb’

module Module1
class Class1
def initialize()
Shared_classes::Getfiles.new() # Invoke code from external file
puts list_clean[0] ==> Error # Try to use the variable
end
end
end

This is the external file called ‘shared_classes.rb’ :

module Shared_classes
class Getfiles
def initialize()
Dir.chdir(“C:\Directory\”)
d = Dir.getwd
listing = Dir.entries(d)
list_clean = []
listing.each { |e|
if File.ctime(e).to_i > 1326718641 # Get all the newer files
list_clean = list_clean.push(e)
end
}

end

end
end

I was expecting that the result of shared_classes.rb (list_clean) could
be used in my file. But not.
How can I use values from variables from external .rb files?

Thnx

On Mon, Jan 23, 2012 at 2:18 PM, Ronnie Aa [email protected] wrote:

end

list_clean = list_clean.push(e)
Like this the code doesn’t work, the variable ‘list_clean’ is not
transferred… // picked up by my file…
How do I solve this??

I guess you want list_clean to be an attribute of the class no a local
variable of the initialize method:

class Getfiles
attr_reader :list_clean

def initialize
[…]
@list_clean = []
[…]
end
end

And from the other class:

module Module1
class Class1
def initialize
shared = Shared_classes::Getfiles.new()
puts shared.list_clean[0]
end
end
end

Jesus.

Thanx for your advice, Jesús!