Home >> Computers >> Programming >> Languages >> Ruby


  Comparison and Review
       

  Articles
Books
  FAQs, Help, and Tutorials
Software
   


Ruby occurs as reflective, object-oriented programming language. It combines syntax inspired by Ada and Perl with Smalltalk-like object-oriented features, and besides shares a few features by owning Python, Lisp, Dylan and CLU. Ruby occurs as lone-pass interpreted language.

History
A language was created by Yukihiro "Matz" Matsumoto, who started working on Ruby on February 24, 1993 and released it to the public in 1995.

"Ruby" was known as fallowing the colleague's birthstone. Suitably, a title reflects a language's Perl heritage. Pearl is the birthstone of June while a ruby is the birthstone of July (suggesting progression). Although Matsumoto was not caring of it at a instance, around typography, pearl and ruby come descriptors inside the deprecated hierarchy for nature and severity size.

As of September 2005, the latest stable version is Ace.Viii.Trio. Ruby Single.Ix (by using a few major changes) is likewise inside development.

Philosophy
Matz's primary project consideration is to produce software engineer happy by reducing a lowly function it must run, as a consequence a information of adept user interface design.[http://www.informit.com/articles/article.asp?p=18225] He stresses that systems design needs to emphasize human being, like than computer, needs[http://www.artima.com/intv/ruby4.html] :

Ruby is said to watch a principle of least surprise (POLS), meaning that a language often behaves intuitively or even when the software engineer assumes it should. the sentence did non originate using Matz &, typically speaking, Ruby additional closely follows a paradigm better termed when 'Matz' Least Surprise' though, pleasantly, several coder use at times uncovered it to become or so their have mental model too.

Semantics
Ruby is object-oriented: as of information is an object, including types more languages designate primitive like integers. Each work occurs as method. Known as values (variables) designate information to objects, non a objects themselves. Ruby supports inheritance with dynamic dispatch, mixins and singleton methods (belonging to, and defined for, one instance rather than being defined on the class). Though Ruby doesn't trend lines multiple inheritance, classes can import modules as mixins. Procedural syntax is supported, however all about liquidate Ruby procedurally (that is, outside of the scope of the particular object) is actually done to an Object case known as 'main'. Since this class is parent to each more class, a changes get seeable to 100% classes & objects.

Ruby has been described as a multi-paradigm programming language: it allows you to program procedurally (defining functions/variables outside classes makes the two a share of the root, 'self' Object), by using object orientation (all about is an object) or even functionally (it has anonymous functions, closures, and continuations; statements all keep close at h& values, and functions link to a go evaluation). It has rich trend lines for introspection, reflection and meta-programming.

Based on data from a Ruby FAQ, "If you like Perl, you will like Ruby and be right at home with its syntax. If you like Smalltalk, you will like Ruby and be right at home with its semantics. If you like Python, you may or may not be put off by the huge difference in design philosophy between Python and Ruby/Perl."

Implementations
Ruby has ii independent implementations: a official Ruby interpreter, which is the most widely utilized, & JRuby, the Java-based implementation. A Ruby interpreter has been ported to numerous platforms, including Unix, Microsoft Windows, DOS, Mac OS X, OS/2, Amiga and many thomas more. A Ruby distribution too includes "IRB", an interactional command-line interpreter which may be utilized to trial code quickly.

Licensing terms
Ruby is distributed disjointedly under a free and open source licenses GPL and Ruby License [http://www.ruby-lang.org/en/LICENSE.txt].

Features

object-oriented exception handling iterators and closures (based on passing blocks of code) native, Perl-like regular expressions at the language level operator overloading automatic garbage collecting highly portable multi-threading on all platforms DLL/shared library dynamic loading on virtually all platforms. self-contemplation, reflection and meta-programming big standard library supports dependency injection continuations & generators (examples within RubyGarden: [http://www.rubygarden.org/ruby?Continuations continuations] & [http://www.rubygarden.org/ruby?RubyFromPython generators])

Ruby presently lacks trend lines for Unicode, though it has partial trend lines for UTF-8.

Possible surprises

Although Ruby's project is guided per principle of least surprise, naturally, some features differ from either languages like C or Perl:

List that run by having the capital come treated when constants, and so local variables should start out using the lowercase letter. Boolean evaluation of non-boolean data is nonindulgent: Nought, "" & [] come everthing evaluated to avowedly: Around C, a expression Zero ? Unity : Cypher evaluates to Cipher. Within Ruby, still, it yields One, because possibly the total Cipher is considered a "something"; sole zero & treasonably evaluate to faithlessly. The corollary to this rule is that by convention, Ruby methods -- for instance, regular expression searches — return figures, strings, lists etc. in profits, however zip in failure (e.g., mismatch) or even another expression of the negative. To denote swimming point counts, of these must watch by owning the zero digit (99.Cypher) or even an expressed conversion (99.to_f). These are deficient to append the dot (99.) because counts come susceptible to method syntax. Lack of the character ("char") information nature and severity. This might induce surprises once slicing strings: "abc"[0] yields 97 (an whole number, representing a ASCII code of the first character in the string); to obtain "a" have "abc"[0,1] (the substring of length Unity) or even "abc"[0].chr.

A good names of "gotchas" can be observed within Hal Fulton's book A Ruby Way, places 48–64. But, since a names in the book pertains to an older version of Ruby (version I.Sise), a bit of things stand been fixed since a book's publication. For instance, retry okay, works by having patch, until & for, likewise when iterators.

Examples

A few basic Ruby code:

# All about, including misprint, is an object, and so this works: -199.acrylonitrile-butadiene-styrene # 199 "ruby is cool".length # 12 "Rick".stock("c") # 2 "Nice Day Isn't It?".split(//).uniq.sort.join # " '?DINaceinsty"

Collections

Constructing & utilizing an array: the = [1, 'hi', 3.14, 1, 2, [4, 5]] a[2] # 3.14 a.reverse # [[4, 5], 2, 1, 3.14, 'hi', 1] the.flatten.uniq # [1, 'hi', 3.14, 2, 4, 5]

Constructing & applying the hash:

hash = puts hash['fire'] # Prints: hot hash.each_pair run |key, value| puts "#" end # Prints: a water system is wet # fire is hot hash.delete_if # Deletes 'fluids' => 'soaked'

Blocks and iterators

Them syntaxes for creating the code prevent:

launder puts "Hello, World!" end

Passing the prevent as the parameter (to exist as a closure): def remember(&p) @prevent = p end # Invoke the method, returning it the prevent that will require a title. remember # Whenever a instance is correct -- call for a closure! @prevent.call for("Johnny") # Prints "Hello, Johnny!"

Ruby code corresponding to the fragment in the article Python programming language demonstrating closures: def foo(initial_value=Cipher) volt-ampere = initial_value go to Proc.freshly end setter, getter = foo setter.call for(Xxi) getter.call for # => 21

Giving program flow to a prevent provided at the location of the call def bfs(e) letter q = [] e.mark yield e letter q.click e piece non letter q.empty? u = letter q.shift u.edge_iterator launder |v| whenever does'nt five.marked? v.mark yield v letter q.click v end end end bfs(e)

Iterating assibilate enumerations & arrays applying jams: the = [1, 'hi', 3.14] the.apiece # Prints both element (Trinity..Sestet).from each one # Prints a counts Three across 6

Obstructs act using numerous built-constitutional methods: File.open('file.txt', 'w+b') launder |file| file.puts 'Wrote a select few text.' prevent # File automatically closed here

Or IO.readlines('file.txt') launder |line| # Run every line, on this text. end

Applying an enumeration & the prevent to square 1 to 10: (Unity..X).collect => [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Classes
the resulting code defines a class known as Individual. Additionally to 'initialize', a common builder to produce fresh objects, it has 2 methods: a single to override a <=> comparison operator (therefore Array#sort could sort by age) & a more to override a to_s method (and so Kernel#puts may format its output). On this button, "attr_reader" is an lesson of meta-programming in Ruby: "attr" defines getter and setter methods of time variables; "attr_reader": sole getter methods. Too, a survive evaluated statement around a method is its go to value, letting the omission of an expressed 'link to'.

class Person def initialize(title, age) @title, @age = title, age end def <=>(individual) @age <=> human.age end def to_s "#)" end attr_reader :title, :age end class action = [ Person.new("John", 20), Human.freshly("Markus", 63), Individual.recently("Ash", Xvi) ] puts class action.sort.reverse

A above prints triplet list inside reverse age choose: Markus (63) John (Twenty) Ash (Sixteen)

More examples

Supplementary sample Ruby code is available when algorithmic program in the resulting articles: Exponentiating by squaring Linear search

Operating systems

Ruby is available for the as a consequence operating systems:

Virtually all flavours of Unix DOS Microsoft Windows 95/98/XP/NT/2000/2003 Mac OS X BeOS Amiga Acorn RISC OS OS/2 Syllable

More ports will too survive.

Applications
the Ruby Application Archive serves as the repository for a wide range of Ruby applications & libraries, containing to a higher degree a thousand things. Although a total of applications available doesn't match a volume of lesson available in the Perl or Python community, there is a wide range of tools & utilities which help to foster farther development in the language.

An Invitation to Ruby
Many links, review, comparison to other languages, code samples.

Ruby-lang.org
Interpreted, dynamically typed, pure object-oriented, scripting language for fast, easy programming, from Japan. Simple, straightforward, extensible. Many features to process text files and do system management, as in Perl. More elegant than Perl, fewer parentheses than Lisp. Japan has more users of Ruby than Python. [Open Source]

rubyXML
Known as : news, articles, and links on XML development with the Ruby scripting language.

ruby-talk
Ruby mail list log/archive, powered by Shin-ichiro Hara's Ruby smart archiver.

RubyGarden.org
New threads and announcements from the ruby-talk mailing list (AKA comp.lang.ruby).

Ruby Garden
Premier site for tracking many developments: forum; news, includes Ruby News Weekly (like Dr. Dobb's Python-URL!); announcements, future shock, biggest Ruby Wiki, useful surveys, links, people, libraries, ports.

ruby / K.Hiwada
Ruby tips, rb2c Ruby to C converter; experimental Ruby JIT compiler.

Ruby User Groups
Ruby Garden Wiki pages for many RUGs, in many nations, on most continents.

Object Oriented Language 'Ruby'
Many useful, interesting scripts, patches, extension modules, and documentation.

RubyRing
WebRing of Ruby sites, some in/with English material.


Computers: Programming: Languages: Interpreted: Object-Oriented
Computers: Programming: Languages: Object-Oriented: Class-based
Computers: Programming: Languages: Object-Oriented: Pure
Computers: Programming: Languages: Open Source
Computers: Programming: Languages: Reflective
Computers: Programming: Languages: Scripting: Object-Oriented
Computers: Programming: Languages: Smalltalk




© 2005 GeneralAnswers.org