structure of a programme

Y

yves piel

Hello,
I'm a java developper and I want to test Ruby which seems a very
powerfull/easy language :)
I read doc about how to program but I never see something telling me how
to structure a programme...
So in Java there are "packages" witch contains "classes" and when I
launch Java I tell it where are those package so I can access to classes.
Is there something similar in Ruby ? Where can I stock classes than I
wrote ? Must I store them into the same directory (I don't think so...) ?

thank for your help :)
 
R

Robert Klemme

yves piel said:
Hello,
I'm a java developper and I want to test Ruby which seems a very
powerfull/easy language :)
I read doc about how to program but I never see something telling me how
to structure a programme...
So in Java there are "packages" witch contains "classes" and when I
launch Java I tell it where are those package so I can access to classes.
Is there something similar in Ruby ?

In Ruby it's called "Module":

module Foo
class Bar
end
end

x = Foo::Bar.new
x = ::Foo::Bar.new
Where can I stock classes than I
wrote ? Must I store them into the same directory (I don't think so...)
?

You can do that (for example for small projects). You can even define
them in a single file.

If you have a more complex application, the usual approach is to put stuff
into a module and have a file with a similar name:

file foo.rb:

require 'foo/bar'
require 'foo/other'
module Foo
end

file foo/bar.rb:

module Foo
module Bar
class Y
end
end
end

You can also look at the std lib for examples.
thank for your help :)

Kind regards

robert
 
Y

yves piel

Robert said:
In Ruby it's called "Module":

module Foo
class Bar
end
end

x = Foo::Bar.new
x = ::Foo::Bar.new



?

You can do that (for example for small projects). You can even define
them in a single file.

If you have a more complex application, the usual approach is to put stuff
into a module and have a file with a similar name:

file foo.rb:

require 'foo/bar'
require 'foo/other'
module Foo
end

file foo/bar.rb:

module Foo
module Bar
class Y
end
end
end

You can also look at the std lib for examples.




Kind regards

robert
ok, thank you :)

So I can say that the Bar module is a 'subpackage' of the Foo one.
If in the Bar Module I want to have more class, I can create a file :
file: foo/barExt.rb
module Foo
module Bar
class X<Y
end
end
end

Is that right ?

So there are "modules" into modules there are "classes" and "functions"
into classes there are "functions" and "attributes"

:)
 
Y

yves piel

yves said:
ok, thank you :)

So I can say that the Bar module is a 'subpackage' of the Foo one.
If in the Bar Module I want to have more class, I can create a file :
file: foo/barExt.rb
module Foo
module Bar
class X<Y
end
end
end

Is that right ?

So there are "modules" into modules there are "classes" and "functions"
into classes there are "functions" and "attributes"

:)
ok :)
thanks I understand more the struture of ruby.
So there is no analogy between the file structure and the class
hierarchie like in Java. What is the best way (a good way) to structure
..rb files ? I won't put all modules and clases into one file ... so ?
 
R

Robert Klemme

yves piel said:
ok, thank you :)

So I can say that the Bar module is a 'subpackage' of the Foo one.
If in the Bar Module I want to have more class, I can create a file :

You can as well put them into foo/bar.rb
file: foo/barExt.rb
module Foo
module Bar
class X<Y
end
end
end

Is that right ?

So there are "modules" into modules there are "classes" and "functions"

and "modules"
into classes there are "functions" and "attributes"

You can even have attributes in modules.

module Bar
attr_accessor :name
end

class Foo
include Bar
end

Foo.new.name = "something"

The difference between a module and a class is really very small in Ruby.
The main point is that you can have only single inheratance with classes
and you cannot inherit a module but you can include (mixin) an arbitrary
number of modules.

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,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top