Programmering:Ruby Introduktion

Fra Wikibooks, den frie samling af lærebøger
Spring til navigation Spring til søgning

Ruby-programmeringssprogets egenskaber[redigér]

Oversættelse mangler anyone? :)

  • Ruby har en simpel syntax, delvist inspireret af Eiffel og Ada.
  • Ruby har 'exception'-behandlingsegenskaber, ligesom Java eller Python, for at gøre det enkelt at håndtere fejl.
  • Rubys operatorer er syntaktisk sukker for metoderne. Du kan nemt redefinere dem.
  • Ruby er et komplet, rent objekt-orienteret sprog. Det betyder at al data i Ruby er et objekt, ikke som i Python eller Perl, uden som i Smalltalk - uden undtagelser. For eksempel: I Ruby er tallet 1 en instance af klassen Fixnum.
  • Ruby's OO is carefully designed to be both complete and open for improvements. Example: Ruby has the ability to add methods to a class, or even to an instance during runtime. So, if needed, an instance of one class *can* behave differently from other instances of the same class.
  • Ruby features single inheritance only, on purpose. But Ruby knows the concept of modules (called Categories in Objective-C). Modules are collections of methods. Every class can import a module and so gets all its methods for free. Some of us think that this is a much clearer way than multiple inheritance, which is complex, and not used very often compared with single inheritance (other than C++, which has often no other choice due to strong type checking!).
  • Ruby features true closures. Not just unnamed function, but with present variable bindings.
  • Ruby features blocks in its syntax (code surrounded by '{' ... '}' or 'do' ... 'end'). These blocks can be passed to methods, or converted into closures.
  • Ruby features a true mark-and-sweep garbage collector. It works with all Ruby objects. You don't have to care about maintaining reference counts in extension libraries.
  • At skrive C moduler i Ruby er lettere end i Perl og Python, det skyldes delvist en såkaldt "garbage collector", og delvist på grund af API modulet. SWIG interface is also available.
  • Integers in Ruby can (and should) be used without counting their internal representation. There are small integers (instances of class Fixnum) and large integers (Bignum), but you need not worry over which one is used currently. If a value is small enough, an integer is a Fixnum, otherwise it is a Bignum. Conversion occurs automatically.
  • Ruby needs no variable declarations. It uses simple naming conventions to denote the scope of variables. Examples: simple 'var' = local variable, '@var' = instance variable, '$var' = global variable. So it is also not necessary to use a tiresome 'self.' prepended to every instance member.
  • Ruby can load extension libraries dynamically if an OS allows.
  • Ruby features OS independent threading (though simulated). Thus, for all platforms on which Ruby runs, you also have multithreading, regardless of if the OS supports it or not, even on MS-DOS!
  • Ruby is highly portable: it is developed mostly on Linux, but works on many types of UNIX, DOS, Windows 95/98/NT, Mac, BeOS, OS/2, etc.