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

A

Axel Etzold

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
 
R

Ryan Paul

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
 
A

Axel Etzold

Dear Ryan,

That solved it! Thank you very much !

Best regards

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

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,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top