Paganoni <noway@fakenullvoid.com> writes:
> I'm still thinking in Ruby as I did in C++ or Java (as I did in Pascal
> in fact). I tried countless times to learn Ruby concepts, but each
> time they faded away from my mind few hours later. I've read books,
> plenty of articles found on the web but I'm still at loss.
> [...]
> What can I do to improve my ruby skills ?
- learn Smalltalk, or
- learn Scheme or Common-Lisp.
Actually, it may be enough for you to understand the fundamental
difference between the object-oriented programming language you know,
and Ruby, Smalltalk, Scheme, Lisp (and also Python, Perl and PHP):
In C++, Java or Pascal, the variables are typed: you cannot put in a
variable a value of a different type than the one declared for the
variable. This allow the compilers of these languages to _blindly_
apply an operation on the value stored in a variable, as long as this
operation is compatible with the type of that variable. Theorically,
it should be safe enough. In practice, there may be loopholes allowing
to store random bit patterns in variables and thus breaking the
programs (true mostly of C++, but it's easily done in Pascal too, less
easily in Java).
In Ruby, Smalltalk, Scheme, Lisp, etc, it's the _values_ that are
typed, variable are entirely untyped: you can assign any type of value
to any variable. The operations can and do check the type of the
values they're applied on and either implement generic behavior (do
something different for each type of value) or signal an error (at
run-time). Since it is impossible (or very hard) to have invalid bit
patterns in values, it's almost impossible to make programs break
(basically, you can do it when you have access to a "FFI" a Foreign
Function Interface which let you fall back to C-level programming and
all it's unsafeness).
Well to allow for OO polymorphism, statically typed programming
languages may implement partially such run-time value-dependent
dispatching of polymorph operations, so as long as you declare your
variables as being of type some root class, you can store in them
values of any subclass type (notice that in C++ there's no single root
class, you can have as many class hierarchies as you want, so you
cannot store instances of subclasses of another hierarchy. And in any
case, in these programming languages, there are types that are not
classes, the Plain Old Data types such as integer (even if as you know
Java defines corresponding classes).
In dynamically typed programming languages such as Ruby, Smalltalk,
Scheme, Lisp, etc, all the values have a class, even simple integers,
and operations can be dispatched on their class.
So basically, in Ruby (and other dynamically typed programming
languages), all the values are objects, and only objects have a type
(a class). Everything is an object (even classes and methods). And
since variables are not typed, you can define generic operations that
work on more than one type of values, even classes that are not
defined at the time you write the operation. This is what let these
language be very extensible.
--
__Pascal Bourguignon__