Is it possible to call functions from different scripts in a Ruby script?

Dear experienced Rubyists,

I am new to Ruby.
In other script-like softwares (with more limited purposes), such as
Matlab,
it is possible to call functions (i.e., modules),
from other files, and to pass arguments to them,
which allows to spread the code and re-use from different
scripts it.
Is this possible in Ruby , too ? Or is there a
principal constraint from object-oriented programming,
which forbids this ?

Thank you for your help!

Best regards,

Axel Etzold

it sounds like you are talking about importing… you can write functions,
classes, modules, etc, in one file and access them quite easily from
another, using the ‘require’ keyword. Use the word ‘require’, followed by
the name of the file you want to include. you only need the prefix of the
file, (you leave off the .rb part) and you surround the name with single
quotes.

file1.rb

class MyClass1
def myfunc1
puts “function1 called!”
end
end

···

On Sat, 22 May 2004 03:08:44 +0900, Axel Etzold wrote:

Dear experienced Rubyists,

I am new to Ruby.
In other script-like softwares (with more limited purposes), such as
Matlab,
it is possible to call functions (i.e., modules),
from other files, and to pass arguments to them,
which allows to spread the code and re-use from different
scripts it.
Is this possible in Ruby , too ? Or is there a
principal constraint from object-oriented programming,
which forbids this ?

Thank you for your help!

Best regards,

Axel Etzold


file2.rb

require ‘file1’

a = MyClass1.new
a.myfunc1

Dear Ryan,

That solved it! Thank you very much !

Best regards

Axel

···

Am Fre, den 21.05.2004 schrieb Ryan Paul um 20:48:

On Sat, 22 May 2004 03:08:44 +0900, Axel Etzold wrote:

Dear experienced Rubyists,

I am new to Ruby.
In other script-like softwares (with more limited purposes), such as
Matlab,
it is possible to call functions (i.e., modules),
from other files, and to pass arguments to them,
which allows to spread the code and re-use from different
scripts it.
Is this possible in Ruby , too ? Or is there a
principal constraint from object-oriented programming,
which forbids this ?

Thank you for your help!

Best regards,

Axel Etzold

it sounds like you are talking about importing… you can write functions,
classes, modules, etc, in one file and access them quite easily from
another, using the ‘require’ keyword. Use the word ‘require’, followed by
the name of the file you want to include. you only need the prefix of the
file, (you leave off the .rb part) and you surround the name with single
quotes.

file1.rb

class MyClass1
def myfunc1
puts “function1 called!”
end
end


file2.rb

require ‘file1’

a = MyClass1.new
a.myfunc1