[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

structure of a programme

yves piel

3/1/2005 10:33:00 AM

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 :)
4 Answers

Robert Klemme

3/1/2005 12:43:00 PM

0


"yves piel" <yvespielusenet@free.fr> schrieb im Newsbeitrag
news:422444a1$0$3187$636a15ce@news.free.fr...
> 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

yves piel

3/1/2005 1:01:00 PM

0

Robert Klemme wrote:
> "yves piel" <yvespielusenet@free.fr> schrieb im Newsbeitrag
> news:422444a1$0$3187$636a15ce@news.free.fr...
>
>>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
>
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"

:)

yves piel

3/1/2005 3:02:00 PM

0

yves piel wrote:
> Robert Klemme wrote:
>
>> "yves piel" <yvespielusenet@free.fr> schrieb im Newsbeitrag
>> news:422444a1$0$3187$636a15ce@news.free.fr...
>>
>>> 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
>>
> 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 ?

Robert Klemme

3/1/2005 3:36:00 PM

0


"yves piel" <yvespielusenet@free.fr> schrieb im Newsbeitrag
news:42246766$0$31658$636a15ce@news.free.fr...
> Robert Klemme wrote:
> > "yves piel" <yvespielusenet@free.fr> schrieb im Newsbeitrag
> > news:422444a1$0$3187$636a15ce@news.free.fr...
> >
> >>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
> >
> 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