[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Ten Things Every Java Programmer Should Know About Ruby

e

1/30/2005 5:34:00 AM

> Lähettäjä: Navindra Umanee <navindra@cs.mcgill.ca>
> Aihe: Re: Ten Things Every Java Programmer Should Know About Ruby
>
> Florian Frank <flori@nixe.ping.de> wrote:
> > Ruby isn't statically type checked isn't anything new. Variables don't
> > have a type in Ruby, so you can rebind a variable to as many objects as
> > you wish like for example in Scheme.
>
> You can accomplish nearly the same thing in Java by declaring
> variables to be of type Object -- but of course it's still
> strongly-typed.
>
> An object in Java is typed according to its class definition and its
> place in the hierarchy. If you try to do something with an object,
> you must specify which type you are expecting and you must respect
> that definition as well as the actual type of the object. That's type
> safety by your definition.
>
> > If I use "strongly typed" I mean that the language is type safe: There
> > can never be an operation, that is applied to the wrong type. If you try
> > this in Ruby (or Java), an exception is raised (because of dynamic type
> > checking). So Ruby is type safe, and how I understand the phrase,
> > "strongly typed". If you don't agree, try to define "strong typing" in
> > another meaningful way.
>
> As far as I can tell and what all the examples seem to have shown, is
> that Ruby doesn't really have a strict concept of type as most people
> understand it.

You're exactly right. Ruby is /strongly/ typed, meaning that each
object is of a discrete, well-defined type, but it is not strictly
typed in the sense that a variable may hold different types and
a type need not be specified to invoke an operation on an object.

strong/weak = Does the language have discrete types for all objects.
strict/loose = Does the language require type specifiers etc.

Conceptually you should really treat everything as an object: tell
it to do something and see what happens. If you've constructed your
software well, what happens will be what you expected to happen. In
place of things like 'public void foo(type a, otherType b)', you use
run-time reflection, unit testing and overall good engineering :)

> Okay, so object.class will supposedly tell you what class an object
> is... but you can override that method and nobody really cares. So
> Ruby doesn't guarantee anything about type safety.

Well, Ruby does guarantee that you can't call 'foo' on an object that
doesn't implement that method.

> All Ruby cares about is that an object responds to the method that is
> being called on it -- however that object may respond and whatever
> type the object may be is irrelevant.
>
> object.fly() may cause a bird to fly or a pig to eat a fly. It
> doesn't matter. You can of course do the same thing in Java, but you
> have to do it by respecting types i.e. you pretty much have to know
> what you are doing to accomplish this.
>
> An object in Ruby may even change its type from one moment to the
> other. A Socket object may suddenly become a File object.

No, a label for a Socket object (e.g. 'foo' in 'foo = Socket.new') may
point to a different type of an object. The object itself doesn't
change. This is a really important bit.

(Technically it's also possible to modify the Class Socket to
essentially become Class File, but that'd be stupendously poor
design.)

> Duck typing seems the best description so far for what Ruby does. I
> think it really confuses the issue to say that Ruby has strong typing
> in addition to duck typing... unless I'm missing something, which is
> very much possible.
>
> > An example for a type unsafe or weakly typed language would be C, where
> > bad things can happen because of it:
> >
> > (flori@lambda:c 0)$ cat foo.c ; make foo; ./foo
> > main() { printf("%s\n", 666); }
> > Segmentation fault
>
> I agree this is weak typing.
>
> Cheers,
> Navin.

E



7 Answers

Navindra Umanee

1/30/2005 6:25:00 AM

0

E S <eero.saynatkari@kolumbus.fi> wrote:
> You're exactly right. Ruby is /strongly/ typed, meaning that each
> object is of a discrete, well-defined type, but it is not strictly

No offense, but I never said anything of the sort... :-)

I don't know what the definition of type in Ruby is, but as far as I
can tell it is anything but discrete. Types overlap inasmuch as the
method names they respond to overlap. It really only seems to make
sense to talk about methods and not to get hung up on types.

<warning>

A "biggest" type in Ruby (that is, a type that can pass for almost any
type) is probably the object that implements all methods, or simply,
method_missing. However there can be different implementations of
method_missing, and so matters get very murky indeed because the
behaviour of two different biggest types by this definition can be
very different. Probably the biggest *biggest* type is an object that
implements all methods and returns itself each time. Even then it
might not always quack like a duck.

</warning>

It really isn't too clear. :-)

> Well, Ruby does guarantee that you can't call 'foo' on an object that
> doesn't implement that method.

Yes... foo and method_missing.

> No, a label for a Socket object (e.g. 'foo' in 'foo = Socket.new') may
> point to a different type of an object. The object itself doesn't
> change. This is a really important bit.

I hope we can stop talking about variables... please see the example
code I have given in the other emails where an object changes its
type.

Cheers,
Navin.


Alexander Kellett

1/30/2005 12:03:00 PM

0

On Jan 30, 2005, at 7:25 AM, Navindra Umanee wrote:
> I hope we can stop talking about variables... please see the example
> code I have given in the other emails where an object changes its
> type.

giving hopeless corner cases won't really prove the point.
i've been coding in ruby for over 3 years now and have never
had a type magically change. the fact that a socket is an io
and reopen has a fairly magical (and documented) property
of type change doesn't mean that ruby isn't strongly typed.

Alex



Navindra Umanee

1/30/2005 8:43:00 PM

0

Hi Alex,

Alexander Kellett <ruby-lists@lypanov.net> wrote:
> On Jan 30, 2005, at 7:25 AM, Navindra Umanee wrote:
> > I hope we can stop talking about variables... please see the example
> > code I have given in the other emails where an object changes its
> > type.
>
> giving hopeless corner cases won't really prove the point.

Will labelling something a "hopeless corner case" make it go away? :-)

> i've been coding in ruby for over 3 years now and have never
> had a type magically change. the fact that a socket is an io

All it suffices is one counterexample. There are many things that
people think are true that are not proven. One counterexample at any
point, however, and the case crumbles.

> and reopen has a fairly magical (and documented) property
> of type change doesn't mean that ruby isn't strongly typed.

Okay, I don't know if it's magical or not. I admit I haven't looked
at the code. I just assumed you could do this sort of thing by the
way you can pretty much dynamically redefine anything in Ruby.

Cheers,
Navin.


Alexander Kellett

1/31/2005 1:23:00 PM

0

On Jan 30, 2005, at 9:42 PM, Navindra Umanee wrote:
> All it suffices is one counterexample. There are many things that
> people think are true that are not proven. One counterexample at any
> point, however, and the case crumbles.

maybe i'm a hopeless corner case but its my
opinion that an exception doesn't rule out
use of the rule ;)

Alex



dblack

1/31/2005 1:41:00 PM

0

Douglas Livingstone

1/31/2005 8:16:00 PM

0

On Sun, 30 Jan 2005 21:02:56 +0900, Alexander Kellett
<ruby-lists@lypanov.net> wrote:
> On Jan 30, 2005, at 7:25 AM, Navindra Umanee wrote:
> the fact that a socket is an io
> and reopen has a fairly magical (and documented) property
> of type change doesn't mean that ruby isn't strongly typed.

Could the same mechanism make i++ work?

Douglas


dblack

1/31/2005 8:33:00 PM

0