CSC/ECE 517 Fall 2011/ch3 3a oe: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
Basic syntax
= Ruby Introduction =
Ruby is a very flexible language from top to bottom. It can be used as a procedural scripting language or as object oriented language.
 
Ruby is a very flexible language from top to bottom. It can be used as a procedural scripting language for rudimentary scripts, or as object oriented language.
 
== Basic syntax ==


In comparison with some other object-oriented or procedural languages such as Java or C, Ruby has a flexible syntax. Many aspects of a statement that would be required in Java are optional in Ruby.
In comparison with some other object-oriented or procedural languages such as Java or C, Ruby has a flexible syntax. Many aspects of a statement that would be required in Java are optional in Ruby.
Line 12: Line 15:
-Loops and iterators. These constructs all achieve the same result of printing the nubmers 1 through 5. The  
-Loops and iterators. These constructs all achieve the same result of printing the nubmers 1 through 5. The  


for i in 1..5 do puts i.to_s; end
for i in 1..5 do puts i.to_s; end
for i in [1,2,3,4,5] do puts i.to_s; end
for i in [1,2,3,4,5] do puts i.to_s; end
(1..5).each { |i| puts i.to_s }
(1..5).each { |i| puts i.to_s }
[1,2,3,4,5].each { |i| puts i.to_s }
[1,2,3,4,5].each { |i| puts i.to_s }
1.upto(5) { |i| puts i.to_s }
1.upto(5) { |i| puts i.to_s }
1.step(5,1) { |i| puts i.to_s }
1.step(5,1) { |i| puts i.to_s }


Object-Oriented principles
== Object-Oriented principles ==


Everything is an object.
=== Everything is an object ===


In comparison with some other Object Oriented languages, Ruby is a very "pure" language, in that essentially everything is an object. There are no primitive types, and even numbers or literal strings are treated as objects and can have methods run on them.
In comparison with some other Object Oriented languages, Ruby is a very "pure" language, in that essentially everything is an object. There are no primitive types, and even numbers or literal strings are treated as objects and can have methods run on them.
5.times do puts "hello"; end
5.times do puts "hello"; end
"hello".index('e')    ->returns 1
"hello".index('e')    ->returns 1


Unbounded polymorphism
=== Unbounded polymorphism ===


Object types and classes are not checked by the compiler (there is no compiler!) or at runtime. An object's class does not matter as long as it contains all the instance methods and variables that are accessed. This is also known as "Duck Typing," a reference to the famous "Duck test": If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.
Object types and classes are not checked by the compiler (there is no compiler!) or at runtime. An object's class does not matter as long as it contains all the instance methods and variables that are accessed. This is also known as "Duck Typing," a reference to the famous "Duck test": If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.


Dynamic typing
=== Dynamic typing ===


Data types of objects are handled and checked at runtime, as opposed to a statically typed language where they are checked at compile time. In a statically typed language like Java, when you define an array, it must be an array containing elements all of the same type. In dynamically typed languages like Ruby, each element of an array can be any type.
Data types of objects are handled and checked at runtime, as opposed to a statically typed language where they are checked at compile time. In a statically typed language like Java, when you define an array, it must be an array containing elements all of the same type. In dynamically typed languages like Ruby, each element of an array can be any type.


Basic classes
== Basic types/classes ==


Array
=== Array ===
www.ruby-doc.org/core/Array.html
http://www.ruby-doc.org/core/Array.html


An array is an ordered one dimensional vector of objects. The objects in the vector are indexed with consecutive integers starting from zero. Any element in any array can be of any object type. An array is created as below:
An array is an ordered one dimensional vector of objects. The objects in the vector are indexed with consecutive integers starting from zero. Any element in any array can be of any object type. An array is created as below:
@a = ['a', 2, :blah]
@a = ['a', 2, :blah]


The individual elements are accessed as below:
The individual elements are accessed as below:
@a[0]
@a[0]
--> 'a'
--> 'a'


Unlike some other languages, array elements can be accessed by negative indices as well. An index of -1 will retrieve the ultimate element in the array; an index of -2 will retrieve the penultimate element, and so on.
Unlike some other languages, array elements can be accessed by negative indices as well. An index of -1 will retrieve the ultimate element in the array; an index of -2 will retrieve the penultimate element, and so on.
Line 54: Line 57:
array.concat: Returns an array containing the array concatenated with another
array.concat: Returns an array containing the array concatenated with another


Hash
=== Hash ===
www.ruby-doc.org/core/Hash.html
http://www.ruby-doc.org/core/Hash.html


A hash is similar to an array, however it is not ordered and it is indexed by keys, which can be any object type. It functions as a mapping from the key to the value.
A hash is similar to an array, however it is not ordered and it is indexed by keys, which can be any object type. It functions as a mapping from the key to the value.


String
=== String ===
www.ruby-doc.org/core/String.html
http://www.ruby-doc.org/core/String.html


Symbol
=== Symbol ===
http://www.ruby-doc.org/core/Symbol.html
http://www.ruby-doc.org/core/Symbol.html

Revision as of 02:45, 7 October 2011

Ruby Introduction

Ruby is a very flexible language from top to bottom. It can be used as a procedural scripting language for rudimentary scripts, or as object oriented language.

Basic syntax

In comparison with some other object-oriented or procedural languages such as Java or C, Ruby has a flexible syntax. Many aspects of a statement that would be required in Java are optional in Ruby.

-Semicolons at the end of each line are optional. They are generally used only when combining multiple statements on a single line. -A return statement at the end of a method is not necessary. If there is no return statement, the return value of the last executed statement is returned. -Method arguments. Methods can be created that will accept any number of arguments without using overloading in the conventional sense. Methods can be called with or without parentheses around arguments.

In other cases, where there may be only one way to write a statement in Java, for example, there may be nearly limitless ways to do so in Ruby. The programmer is allowed to determine which way works best for the situation at hand.

-Loops and iterators. These constructs all achieve the same result of printing the nubmers 1 through 5. The

for i in 1..5 do puts i.to_s; end
for i in [1,2,3,4,5] do puts i.to_s; end
(1..5).each { |i| puts i.to_s }
[1,2,3,4,5].each { |i| puts i.to_s }
1.upto(5) { |i| puts i.to_s }
1.step(5,1) { |i| puts i.to_s }

Object-Oriented principles

Everything is an object

In comparison with some other Object Oriented languages, Ruby is a very "pure" language, in that essentially everything is an object. There are no primitive types, and even numbers or literal strings are treated as objects and can have methods run on them.

5.times do puts "hello"; end
"hello".index('e')    ->returns 1

Unbounded polymorphism

Object types and classes are not checked by the compiler (there is no compiler!) or at runtime. An object's class does not matter as long as it contains all the instance methods and variables that are accessed. This is also known as "Duck Typing," a reference to the famous "Duck test": If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.

Dynamic typing

Data types of objects are handled and checked at runtime, as opposed to a statically typed language where they are checked at compile time. In a statically typed language like Java, when you define an array, it must be an array containing elements all of the same type. In dynamically typed languages like Ruby, each element of an array can be any type.

Basic types/classes

Array

http://www.ruby-doc.org/core/Array.html

An array is an ordered one dimensional vector of objects. The objects in the vector are indexed with consecutive integers starting from zero. Any element in any array can be of any object type. An array is created as below:

@a = ['a', 2, :blah]

The individual elements are accessed as below:

@a[0]
--> 'a'

Unlike some other languages, array elements can be accessed by negative indices as well. An index of -1 will retrieve the ultimate element in the array; an index of -2 will retrieve the penultimate element, and so on.

Examples of useful instance methods: array.each { block }: iterator that executes the block once for each element in the array array.length: returns the number of elements in the array array.concat: Returns an array containing the array concatenated with another

Hash

http://www.ruby-doc.org/core/Hash.html

A hash is similar to an array, however it is not ordered and it is indexed by keys, which can be any object type. It functions as a mapping from the key to the value.

String

http://www.ruby-doc.org/core/String.html

Symbol

http://www.ruby-doc.org/core/Symbol.html