Ruby programming language
Ruby is a purely object-oriented programming language originally developed for scripting. It combines syntax inspired by Ada and Perl with Smalltalk-like object oriented features, and also shares some features with Python, Lisp and CLU.
Ruby is purely object-oriented: every bit of data is an object, including types that are designated "primitive" in other languages, for example integers. Every function is a method. Named values (variables) designate references to objects, not the objects themselves. Ruby supports inheritance with dynamic dispatch, mixins, and singleton methods (belonging to an instance rather than a class). Though Ruby does not support multiple inheritance, classes can import modules as mixins. While procedural syntax is possible, everything is an object, in the sense of Smalltalk, not Perl. Anything done in Ruby procedurally (ie. outside of the scope of a particular object) is actually done to the Object class. Since this class is parent to every other class, the changes become visible to all classes and objects.
Ruby has also been described as a multi-paradigm programming language: It allows you to program procedurally (defining a function/variable outside a class makes it part of the root 'self' Object), object-orientated (everything is an object) or functionally (anonymous functions, closures, continuations, all expressions return a value, when no return statement is present, functions return the last value evaluated). It has rich support for introspection, reflection and metaprogramming
The language was created by Yukihiro "Matz" Matsumoto, who started working on Ruby on February 24, 1993 and released it to the public in 1995. The current stable version is 1.8.1 (as of February 2004). Note that the name is not an acronym - it is actually a pun on Perl. According to the author, he designed Ruby to follow the principle of least surprise (POLS), meaning that the language should be free from the traps and inconsistencies that plague other languages.
Ruby has two main implementations, the official Ruby interpreter, which is the most widely used and JRuby, a Java-based implementation. The standard interpreter has been ported to many platforms, including Unix, Microsoft Windows, DOS, Mac OS X, OS/2, Amiga, and many more. The Ruby distribution also includes IRB, an interactive command-line interpreter which can be used to quickly test out code.
From the 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."
Ruby is distributed disjointly under the free and open source licences GPL and Ruby License [1].
| Table of contents |
|
2 Examples 3 'water' } # deletes 'water' => 'wet'
4 External links |
Features
Ruby currently lacks support for Unicode, though it has partial support of UTF-8.
constructing and using an array:
Two different ways of making blocks:
Examples
Here are some basic examples of Ruby code:# everything, including literals, is an object, so this works:
-199.abs # 199
"ruby is cool".length # 12
"Rick".index("c") # 2
"Nice Day Isn't It?".split(//).uniq.sort.join # " '?DINaceinsty"
Arrays and Hashes
a = [ 1, 'hi', 3.14, 1, 2, [4, 5] ]
a[2] # 3.14
a.reverse # [[4, 5], 2, 1, 3.14, "hi", 1]
a.flatten.uniq # [1, "hi", 3.14, 2, 4, 5]
constructing and using a hash:h = {'water' => 'wet', 'fire' => 'hot'}
puts h['fire']
h.each_pair do |key, value|
puts "#{key} is #{value}"
end
# water is wet
# fire is hot
h.delete_if { |k,v| k == 'water' } # deletes 'water' => 'wet'
Blocks and Iterators
{ puts "Hello, World!" }
do puts "Hello, World" end
passing a block as a parameter (closure concept):
def func name
yield name
end
# call the function with a name and a block
func("John") do |name|
puts "Hello, #{name}"
end
# prints "Hello, John"
iterating through an array using closures:
a = [1, 'hi', 3.14]
a.each { |item| puts item } # prints each element
3.upto(6) { |num| puts num } # prints the numbers 3 to 6
Iterators are part of almost every built-in class:
IO.readlines('file.txt') do |line|
# .. process each line here
end
Using map to calculate squares from 1 to 10:
(1..10).to_a.map { |x| x*x }
=> [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]Classes
The following code defines a class named Person in which the initialize method is a constructor (it is called when creating a new object), and two methods: one overloading the <=> comparasion operator (so Array#sort can sort by age) and the to_s (so Kernel#puts knows how to format the output) method. The attr_reader is an example of metaprogramming in ruby, it defines read/write methods for instance variables (in this case only read methods). The code then creates an array called group with three elements and prints it sorted by age in reverse order.class Person
def initialize(name, age)
@name, @age = name, age
end
def <=>(person)
@age <=> person.age
end
def to_s
"#{@name} (#{@age})"
end
attr_reader :name, :age
end
group = [ Person.new("John", 20),
Person.new("Markus", 63),
Person.new("Ash", 16)
]
puts group.sort.reverse
This code generates the following output:
Markus (63)
John (20)
Ash (16)
More Ruby code is available in the form of sample algorithm implementations in the following articles:
External links
Programming languages
Ada | AWK | BASIC| C | C++ | C# | COBOL | ColdFusion | Common Lisp | Delphi | Fortran | IDL | Java | JavaScript | Lisp | Perl | PHP | Prolog | Pascal | Python | SAS | SQL | Visual Basic | More programming languages
Edit this template