User:Pandawelch/Sandbox

From Wikipedia, the free encyclopedia
Jump to: navigation, search

Ruby[edit]

Ruby
Ruby-logo-notext.png
Paradigm(s) functional, imperative, strict object-oriented (class-based)
Designed by Yukihiro Matsumoto
Appeared in 1995
Stable release 1.8.6 / March 12, 2007
Influenced by Smalltalk, Perl, Lisp, Python, CLU, Eiffel, Dylan
OS Cross-platform
Website www.ruby-lang.org


Ruby is a reflective, dynamic, object-oriented programming language. It combines syntax, semantics and paradigms from multiple other programming languages including Smalltalk, Pyton, List, Dylan and CLU. Ruby is a single-pass interpreted language and is written in C. Ruby was released in 1995 by Yukihiro Matsumoto.


History[edit]

Yukihiro Matsumoto

Ruby was created by Yukihiro Matsumoto, a Japanese computer scientist. Born in the Osaka prefecture, he was a self-taught programmer until the end of high-school. He graduated from Tsukuba University with an information science degree[1]. He wanted a language that was “… more powerful than Perl, and more object-oriented than Python[2]."

Ruby was released to the public in 1995 and has grown in popularity since. The TIOBE (The Importance of Being Earnest) index for November 2007 shows Ruby taking ninth most ‘popular’ place[3]. The index is ranked by number of web pages dedicated to each individual programming language, using popular search engines such as Google and Yahoo[4].

Ruby is regarded as a ‘pure’ object oriented programming language, because everything written in it is treated as an object, from single integers to blocks, classes, prototypes, etc[5]. Ruby, among other ‘pure’ object-oriented languages, was specifically designed to enforce object-oriented methods. Ruby is also referred to as a dynamic and reflective programming language. ‘Dynamic’ is a term used to describe high level programming languages that perform activities at runtime what other languages may not perform at all. ‘Reflective’ means that a computer language is able to be modified while in the process of being executed.


Ruby is a single-pass interpreted language, meaning that code is entered and interpreted one line at a time. There is an online Ruby platform available for free download and distribution at http://www.ruby-lang.org/en/downloads/.

Syntax[edit]

The syntax used in Ruby is similar to that of Python and Perl. Class and method definitions are defined by reserved keywords.


Datatypes and Declaration[edit]

In Ruby, everything is treated as an object, including classes and integers. See the example below:

class Numeric
def plus(x) #making method in class Numeric
    self.+(x)
  end
end
 
y = 5.plus 6  # y now equals 11

By making a method in the class Numeric, the user is able to use the plus method of the number 5.


Ruby uses all common datatypes except for the character or char datatype. Datatypes are not set, but re-defined when needed. Everything in Ruby has a second boolean value. Everything in Ruby is true, unless it is an express nil or false.

a = 1
=> 1
 
b = 2
=> 2
 
 
c = "Hello"
=> "Hello"
 
d = nil
=> nil
 
 
f = if b
..      true #if b is 'true', which it is because it is not expressly false
.. else
..      false #otherwise
.. end
=>      true
 
g = if d
..      true 
.. else
..      false
.. end
=>      false
 
g = if b > a
..	true
.. else
..	false
.. end
=>	true


In Ruby a symbol prefix to variables indicates their scope. Also, an initial capital letter for a variable indicates a constant. However methods are permitted capital letters[6]. This can result in some confusion.

$Constant = 10 #global constant
@name = "One"  #instance variable
@@type = "New" #class variable
 
def Constant
.. 11
.. end
 
Constant
=> 10
Constant()
=> 11

Conditionals[edit]

Ruby makes use of multiple conditionals including

  • If;
  • Unless, the opposite of If;
  • If-elsif-else;
  • Short-If, useful for space-saving concatenation.
  • Case, a switch;


#Example of If statement
a = 5
if a == 7
..    a = 4
.. end
 
puts a  #returns 5 since If-block is not executed
 
#alternative code
 
if a == 7 then a = 3 end
 
#Example of Unless statement
 
a = 5
unless a == 4
  a = 7
end
 
puts a  #prints 7 since the if-block is executed
 
#Example of If-elsif-else statement
#NOTE elsif not 'elseif' or 'else if'
 
if x=1
..    #code block
 elsif x=2
..    #code block
 elsif x=3
..    #code block
 else
..    #code block
 end
 
#Example of a Case Statement
 
case @age
	when 0 .. 2
	  "baby"
	when 3 .. 6
	  "little child"
	when 7 .. 12
	  "child"
	when 13 .. 18
	  "youth"
	else
	  "adult"
	end


Other Control Structures[edit]

Ruby uses several loop styles. These include[7]:

  • While;
  • Until; and
  • Other complex control structures.
#Example of a while loop
 
while not_over
..  #code body will be repeated while not_over is true
.. end
 
#Example of an Until loop
 
until finished
..  #code body to be repeated until finished is true
.. end

Class and Method Declaration[edit]

To declare a method, the keyword def is used, with the method name following after, as well as arguments inside a pair of parentheses. A default value for the arguments can be set. Here is an example of a simple greeter class:

class Greeter
def initialize(name = "world")
 
		@name = name
 
end
def sayhello
		puts "Hello #{@name}!"
end
end

The constructor method is named initialize. In this example, if no name is entered then “Hello world!” will be printed to the terminal.

The greeter class initialize can be called in this way:

# Extends above example
g = Greeter.new("George") #creates a ‘Greeter’ object, g
 
g.say_hello
=>Hello George!	#return to screen

Code Examples[edit]

Below are some examples of Ruby's extreme object-oriented nature, as well as syntax.

#applying a method to a number
5.times { print "Hello User." }
=> Hello User.Hello User.Hello User.Hello User.Hello User.
 
#some of ruby's predefined methods:
name = "matz"
=> "matz"
 
name.capitalise
=> "Matz"
 
name.reverse
=> "ztam"
 
x = 40
=>40
 
x.to_s  #changing the contents of x into a string
=>"40"  
 
array = [12, 45, 2]
=> [12, 45, 2]
 
array.max
=> 45
 
array.sort
=> [2, 12, 45]

Benefits[edit]

  • Ruby is an extremely object-oriented programming language. As the programmer can refer to nearly everything as an object the programmer is able to easily manipulate classes, variables in the same way one would an object.
  • Ruby was designed with simplicity in mind. See below the code required to open the calc.exe application in ruby as compared to in C#, a popular programming language[8].

Ruby[edit]

# Launch calc.exe in Ruby
 
require "win32/guitest"
include win32::GuiTest
 
run_cmd "calc.exe"

C#[edit]

// Launch calc.exe in C#
 
using System;
using System.Diagnostics;
 
namespace MyNameSpace
{
    class MyClass
    {
        static void main ()
        {
            Process.Start("calc.exe");
        }
    }
}
  • Typical embedded classes and methods in other programming languages are easily modifiable in Ruby. Click here to see an example of manipulating a class to have the .plus method for an integer or point datatype.

Weaknesses[edit]

  • Ruby is extremely complex. Matsumoto has often said that "Ruby is simple in appearance, but is very complex inside, just like our human body[9]". The amount of time required to fully understand Ruby is significantly higher that that of other programming languages.
  • Ruby is inferior in terms of speed when compared to compiled languages and other major scripting languages (for example Perl and Python)
  • Lack of strict syntax can result in confusing or unexpected results. An example is the use of parentheses when declaring a method with argument/s, as the parentheses are not required. However with two or more arguments an ambiguous meaning of code may arise.


References[edit]

External Links[edit]