Von C und C++ zu Ruby
Es ist schwer eine Liste von Dingen zusammen zu stellen, die die Unterschiede zwischen Ruby und C oder C++ beschreibt, weil es so ein großer Unterschied ist. Ein Grund hierfür ist, dass die Laufzeitumgebung von Ruby dem Entwickler soviel abnimmt. Ruby scheint so weit wie möglich vom not hidden mechanism (Keine versteckten Mechanismen) Prinzip der Sprache C abzuweichen. Das Anliegen von Ruby ist es, dem Entwickler die Arbeit zu erleichtern, was dazu führt, dass die Laufzeitumgebung entsprechend mehr Aufgaben übernimmt. Wenn sie ihren Sourcecode nicht gerade profilieren und optimieren, brauchen sie sich mit Ruby nicht darüm kümmern, einen Compiler glücklich zu machen.
Dies vorausgesetzt, dürfte auch klar sein, dass ihr Programmcode deutlich langsamer ausgeführt wird, als vergleichbarer C oder C++ Code. Gleichzeigt werden sie sich wundern wie schnell sie eine Ruby-Applikation entwickeln und ablaufen lassen können. Außerdem werden sie feststellen, wie wenige Zeilen Code hierfür notwendig sind. Ruby ist viel einfacher als C++ es wird sie faul machen.
Ruby ist im Gegensatz zur statischen Typisierung dynamisch getypt. Die Laufzeitumgebung erledigt hier so viel wie möglich zur Laufzeit. Ruby is dynamically typed, rather than statically typedthe runtime does as much as possible at run-time. For example, you dont need to know what modules your Ruby program will link to (that is, load and use) or what methods it will call ahead of time.
Happily, it turns out that Ruby and C have a healthy symbiotic relationship. Ruby supports so-called extension modules. These are modules that you can use from your Ruby programs (and which, from the outside, will look and act just like any other Ruby module), but which are written in C. In this way, you can compartmentalize the performance-critical parts of your Ruby software, and smelt those down to pure C.
And, of course, Ruby itself is written in C.
Similarities with C
As with C, in Ruby,...- you may program procedurally if you like (but it will still be object-oriented behind the scenes).
-
most of the operators are the same (including the compound
assignment and also bitwise operators). Though, Ruby doesnt
have
++or--. -
youve got
__FILE__and__LINE__. -
you can also have constants, though theres no special
constkeyword. Const-ness is enforced by a naming convention instead names starting with a capital letter are for constants. - strings go in double-quotes.
- strings are mutable.
-
just like man pages, you can read most docs in your
terminal windowthough using the
ricommand. - youve got the same sort of command-line debugger available.
Similarities with C++
As with C++, in Ruby,...-
youve got mostly the same operators (even
::).<<is often used for appending elements to a list. One note though: with Ruby you never use->its always just.. -
public,private, andprotecteddo similar jobs. -
inheritance syntax is still only one character, but its
<instead of:. -
you may put your code into modules, similar to how
namespacein C++ is used. - exceptions work in a similar manner, though the keyword names have been changed to protect the innocent.
Differences from C
Unlike C, in Ruby,...- objects are strongly typed (and variable names themselves have no type at all).
- theres no macros or preprocessor. No casts. No pointers (nor pointer arithmetic). No typedefs, sizeof, nor enums.
- there are no header files. You just define your functions (usually referred to as methods) and classes in the main source code files.
- theres no #define . Just use constants instead.
- as of Ruby 1.8, code is interpreted at run-time rather than compiled to any sort of machine- or byte-code.
- all variables live on the heap. Further, you dont need to free them yourselfthe garbage collector takes care of that.
- arguments to methods (i.e. functions) are passed by reference, not by value.
-
its
require 'foo'instead of#include <foo>or#include "foo". - you cannot drop down to assembly.
- theres no semicolons ending lines.
-
you go without parentheses for
ifandwhilecondition expressions. - parentheses for method (i.e. function) calls are often optional.
-
you dont usually use bracesjust end multi-line constructs
(like
whileloops) with anendkeyword. -
the
dokeyword is for so-called blocks. Theres no do statement like in C. - the term block means something different. Its for a block of code that you associate with a method call so the method body can call out to the block while it executes.
- there are no variable declarations. You just assign to new names on-the-fly when you need them.
-
when tested for truth, only
falseandnilevaluate to a false value. Everything else is true (including0,0.0, and"0"). -
there is no
charthey are just 1-letter strings. - strings dont end with a null byte.
- array literals go in brackets instead of braces.
- arrays just automatically get bigger when you stuff more elements into them.
- if you add two arrays, you get back a new and bigger array (of course, allocated on the heap) instead of doing pointer arithmetic.
-
more often than not, everything is an expression (that is, things
like
whilestatements actually evaluate to an rvalue).
Differences from C++
Unlike C++, in Ruby,...- theres no explicit references. That is, in Ruby, every variable is just an automatically dereferenced name for some object.
- objects are strongly but dynamically typed. The runtime discovers at runtime if that method call actually works.
-
the constructor is called
initializeinstead of the class name. - all methods are always virtual.
- class (static) variable names always begin with @@ (as in @@total_widgets ).
- you dont directly access member variablesall access to public member variables (known in Ruby as attributes) is via methods.
-
its
selfinstead ofthis. - some methods end in a ? or a !. Its actually part of the method name.
- theres no multiple inheritance per se. Though Ruby has mixins (i.e. you can inherit all instance methods of a module).
- there are some enforced case-conventions (ex. class names start with a capital letter, variables start with a lowercase letter).
- parentheses for method calls are usually optional.
- you can re-open a class anytime and add more methods.
- theres no need of C++ templates (since you can assign any kind of object to a given variable, and types get figured out at runtime anyway). No casting either.
-
iteration is done a bit differently. In Ruby, you dont use a separate iterator object
(like
vector<T>::const_iterator iter) but instead your objects may mixin theEnumeratormodule and just make a method call likemy_obj.each. -
theres only two container types:
ArrayandHash. - theres no type conversions. With Ruby though, youll probably find that they arent necessary.
- multithreading is built-in, but as of Ruby 1.8 they are green threads (implemented only within the interpreter) as opposed to native threads.
- a unit testing lib comes standard with Ruby.
rubyforge.org rubyforge.org rubyforge.org forum.ruby-portal.de radiantcms.org rubyforge.org rubyforge.org tryruby.hobix.com