CSC/ECE 517 Fall 2011/ch3 3a av: Difference between revisions
(16 intermediate revisions by 2 users not shown) | |||
Line 193: | Line 193: | ||
The user need not specify any return value or have an explicit ''return'' for a function. | The user need not specify any return value or have an explicit ''return'' for a function. | ||
The last statement in the function is the return value. And all statements return a value. The function may return ''nil''. Here, in Ruby, ''nil'' does not mean ''null''. ''nil'' is also an object. | The last statement in the function is the return value. And all statements return a value. The function may return ''nil''. Here, in Ruby, ''nil'' does not mean ''null''. ''nil'' is also an object.<br>[http://rubylearning.com/satishtalim/writing_own_ruby_methods.html This] is really helpful to learn more about methods in Ruby. | ||
==Dynamic Typing== | ==Dynamic Typing== | ||
Line 226: | Line 226: | ||
[1,3,6,8].class | [1,3,6,8].class | ||
>>Array | >>Array | ||
<br> | |||
[http://www.sitepoint.com/typing-versus-dynamic-typing/ Here] is something we got off the internet which explains Dynamic typing well although its not particular to Ruby. | |||
==Arrays== | ==Arrays== | ||
Line 267: | Line 269: | ||
puts n | puts n | ||
>> 10 | >> 10 | ||
==Strings== | |||
A [http://www.rubyist.net/~slagell/ruby/strings.html String] in Ruby may be double-quoted ("...") or single-quoted ('...'). | |||
However, double-quoting and single-quoting have different effects in some cases. A double-quoted string allows character escapes by a leading backslash, and the evaluation of embedded expressions using #{}. A single-quoted string does not interpret this. This is shown in the example below: | |||
string1 = "abc" | |||
>> "abc" | |||
string2 = 'efg' | |||
>> "efg" | |||
puts "a\nb\nc" | |||
>>a | |||
>>b | |||
>>c | |||
>>nil | |||
puts 'a\nb\n' | |||
>>a\nb\nc | |||
>>nil | |||
puts "abcd #{5*3} efg" | |||
>>"abcd 15 efg" | |||
puts var = " abc " | |||
>>" abc " | |||
puts "1234#{var}5678" | |||
>>"1234 abc 5678" | |||
Ruby strings can be concatenated using + operator. A string can be repeated multiple times using * operator. The example below shows the use of + and * operators. Ruby string handling is more powerful than C. Since Ruby, provides support for regular expressions, string handling and pattern matching can be made very efficiently and easily. | |||
puts "foo" + "bar" | |||
>>"foobar" | |||
puts "foo" * 2 | |||
>>"foofoo" | |||
===Symbols=== | |||
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ Symbols] in Ruby are Strings, just with an important difference, Symbols are immutable while Strings are not. Mutable objects can be changed after assignment while immutable objects can only be overwritten. Ruby is quite unique in offering mutable Strings, which adds greatly to its expressiveness. However mutable Strings can have their share of issues in terms of creating unexpected results and reduced performance. It is for this reason Ruby also offers programmers the choice of Symbols. | |||
Symbols need not contain only alphanumeric characters, by using quotes you can not only use the same characters as you would in a String, but also interpolate Symbols as well. Syntactically, a Symbol needs a colon : prepended. Symbols are actually so similar to Strings that converting between the two is very simple and consistent. | |||
"hello" | |||
:hello | |||
"hello world" | |||
:"hello world" | |||
bang = "!" | |||
"hello world#{bang}" # => "hello world!" | |||
:"hello world#{bang}" # => :"hello world!" | |||
<BR> | |||
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ This] and [http://www.troubleshooters.com/codecorn/ruby/symbols.htm This] delve deeply into the difference between string and symbol. Well they are interesting worth reading atleast once. | |||
==Hashes== | ==Hashes== | ||
Line 474: | Line 536: | ||
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x). More about currying may be learnt from [http://en.wikipedia.org/wiki/Currying here]. Currying is a very interesting feature and together with closures, it provides a flexibility to ruby at ease which is not easily achievable in java or c++. | So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x). More about currying may be learnt from [http://en.wikipedia.org/wiki/Currying here]. Currying is a very interesting feature and together with closures, it provides a flexibility to ruby at ease which is not easily achievable in java or c++. | ||
<br> [http://asemanfar.com/Currying-in-Ruby Here] is a set of nice programming examples in Ruby to explain/implement currying. | |||
==Conclusion== | ==Conclusion== | ||
Line 480: | Line 543: | ||
==See Also== | ==See Also== | ||
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]<br> | [http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]<br> | ||
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]<br> | [http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]<br> | ||
Line 499: | Line 563: | ||
==References== | ==References== | ||
[http://www.ruby-doc.org/docs/ProgrammingRuby/ "Programming Ruby - The Pragmatic Programmer's Guide"]<br> | [http://www.ruby-doc.org/docs/ProgrammingRuby/ "Programming Ruby - The Pragmatic Programmer's Guide"]<br> | ||
Line 517: | Line 580: | ||
[http://en.wikipedia.org/wiki/Interactive_Ruby_Shell Interactive Ruby Browser]<br> | [http://en.wikipedia.org/wiki/Interactive_Ruby_Shell Interactive Ruby Browser]<br> | ||
[http://www.ruby-doc.org/core-1.9.2/Array.html Array class in Ruby]<br> | [http://www.ruby-doc.org/core-1.9.2/Array.html Array class in Ruby]<br> | ||
[http://www.rubyist.net/~slagell/ruby/strings.html String]<br> | |||
[http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ Symbols]<br> | |||
[http://www.ruby-doc.org/core-1.9.2/Hash.html Hash class in Ruby]<br> | [http://www.ruby-doc.org/core-1.9.2/Hash.html Hash class in Ruby]<br> | ||
[http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ Blocks, Procs and Lambdas]<br> | [http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ Blocks, Procs and Lambdas]<br> | ||
[http://www.csc.ncsu.edu/faculty/efg/517/f11/schedule CSC517 Class notes] | [http://www.csc.ncsu.edu/faculty/efg/517/f11/schedule CSC517 Class notes] |
Latest revision as of 00:42, 15 October 2011
Introduction to Ruby
Ruby is a dynamic, reflective, object oriented, single-pass interpreted programming language. Ruby was influenced by Perl, Smalltalk, Eiffel and Lisp. It supports functional, imperative, reflective, object oriented and many other programming paradigms. Since Ruby was designed to incorporate the best features of scripting and Object oriented languages, Ruby emerged to be a very powerful language with native support for many features that are not in the 'nature' of other languages like Java and C++.
Ruby is said to incorporate the "Principle of Least Surprise" or "Principle of Least Astonishment".
History
Ruby was created in 1993 in Japan, by Yukihiro "Matz" Matsumoto. The intent of developing Ruby was to have a new language that balances functional programming and iterative programming. Matsumoto has also stated that he wanted a scripting language that was more powerful than Perl and more Object Oriented than Python.
Naming
The terms "Coral" and "Ruby" were the two proposed names for the language. Matsumoto's choose the term "Ruby" because it was one of his colleague’s birthstone.
Releases and Versions
Ruby 0.95 was the first public release in 1995. Three more versions were released immediately.
Ruby 1.0 was released in 1996.
Ruby 1.3 was the next release in 1999.
Since then several versions of Ruby have been released with added concepts and features.
The recent stable version is Ruby 1.9.2 which has significant improvements over Ruby 1.8 series.
Currently, Ruby 1.9.3 is under development with a dual-license of Ruby and BSD.
Ruby on Rails
With the release of Ruby on Rails in 2005, an MVC based web development framework written in Ruby, the new language gained mass acceptance and became famous.
In 2006, active user groups were formed in the world's major cities and several Ruby related conferences were held, making it one of the most popular and widely acceptable language. In 2007, TIOBE programming language popularity index state Ruby as the 10th most popular language.
Features
Ruby is a purely object oriented language. There are no primitives. Everything is an object. For example, Integers, Strings, Arrays are all objects.
Variables in Ruby are not assigned a type. The programmer need not declare the variable with its type before use. The type of the variable is given the type of the value it is assigned. Unlike statically types languages, there is no static compile time type checking in Ruby.
Ruby supports functional programming paradigm by having blocks and closures. Blocks and Closures are explained in detail below.
Ruby supports built-in iterators that are used to traverse over a collection. Built-in iterators make programming easier and code shorter.
5. Multiple Inheritance through Mixins
The disadvantages of multiple inheritance are overcome in Ruby through the use of Modules and Mixins. These are similar to interfaces in Java.
6. Unbounded Polymorphism - "Duck Typing"
A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it. This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work. This is unbounded polymorphism, which can only be found in dynamically typed languages.
7. Reflection
Reflection is referred to be the ability to introspect or examine the aspects of the program from within the program itself. Reflection allows program entities to discover things about themselves through introspection. For example, an object can ask what its methods are, and a class can tell what its ancestors are. Using refection, we can examine particular objects and decide which for their methods to call at run-time, even if the class of the object didn't exist when we first wrote the code. The program can be modified at run-time.
Metaprogramming means writing code that writes code. This allows us to modify the behavior of a program at run time. Ruby has several metaprogramming facilities.
9. Syntax level Perl compatible for regular expressions
Ruby is a scripting as well as programming language. It is strongly influences by Perl and provides Perl compatible regular expression suport.
10. Built-in support for certain Design Patterns
Ruby also provides support for certain design patterns that programmers can use to solve the recurring design problems.
11. Mark-and-Sweep garbage collection
Ruby performs automatic mark-and-sweep garbage collection and frees the unreferenced memory. The programmer need not worry about memory management.
The threading features that Ruby provides are independent of the underlying Operating System. This provides a great deal of flexibility.
13. Easy interface to C modules
Ruby provides interface to C modules which help programmers integrate C with Ruby.
14. Portable easily
Ruby was developed on GNU/Linux. It is portable and works on several types of UNIX, DOS, BeOS, OS/2, Mac OS X, Windows 95/98/Me/NT/2000/XP/Vista/7, etc.
Downloads and Installation
The main website for Ruby is http://www.ruby-lang.org/en/.
Ruby downloads and installation instructions can be found at http://www.rubylang.org/en/downloads/. Ruby can be installed by either building it from source or by using the one-click Ruby installer for Windows. Using the one-click Rails Installer for Windows, one can install Ruby and Rails together.
One can also use Ruby online at http://www.tryruby.org/.
Ruby vs Java
Here is a little table that highlights the key differences between java and ruby. Many of these exact difference maybe shared by Ruby and other languages like C,C++ etc.
Ruby | Java |
---|---|
Interpreted language | Compiled language |
Dynamic Typing. | Static typing. |
Purely OO. Everything is an object | There are primitives and objects. |
Unbounded Polymorphism | Inheritances and interfaces used. |
Native support for Regular Expressions | Not native support; java.util.regex provides it. |
Nil is an object which says there is no object | Null indicates the absence of objects |
Methods calls are treated as messages at runtime | Method calls are compiled |
Methods are passed to the objects | Objects are passed to the methods |
Native support for hashes | Hashes are suported by Collections framework |
Interactive Ruby Browser - IRB
Once Ruby is installed, code can directly be evaluated using the IRB. IRB is an interactive command-line interpreter which can be used to test code quickly. The result of execution of the code is immediately returned in the IRB. It can be started in two ways:
1. Directly open the IRB by clicking on the Interactive Ruby icon.
2. In the command prompt, type "irb" to start a new IRB session.
Other Editors for Ruby
Ruby is supported by Eclipse. It has an exclusive Ruby perspective. However, the eclipse plug-in for ruby has to be installed.
RubyMine is another Ruby editor which is commonly used for Ruby on Rails - Web development Framework. Rubymine is considered to be the standard platform for developing applications in Ruby on Rails.
Basics
The .rb extension is used to save Ruby files. The Ruby programs can be run by the command ‘ruby filename.rb’
The files can be made executable on UNIX systems (without typing ruby) by adding the shebang line as the first line in the file. The path to the ruby command is specified here.
#!/usr/bin/ruby
The “Hello, world” program can be written as
puts “Hello, world!” >>Hello, world!
Functions
Functions are defined by the keyword def. Ruby does not use { } parenthesis to specify scope. Instead, the keyword end is equivalent to the closing parenthesis.
def fivetimes(i = 5) i.times do puts "Ruby" end end fivetimes >>Ruby >>Ruby >>Ruby >>Ruby >>Ruby
The arguments to a function can be initialized in the function definition itself. The arguments can be specified without ( ) parentheses. This means parentheses in a function call are optional. We can write fivetimes ( 4 ) or fivetimes 4 .
Function arguments can be aggregated –
def greeting(*names) puts "Hello #{names.join(', ')}" end greeting('Sam', 'Peter', 'Derek') >>Hello Sam, Peter, Derek
The user need not specify any return value or have an explicit return for a function.
The last statement in the function is the return value. And all statements return a value. The function may return nil. Here, in Ruby, nil does not mean null. nil is also an object.
This is really helpful to learn more about methods in Ruby.
Dynamic Typing
One of the reasons that makes Ruby easier to program is the fact that the programmer need not specify the type for any variable. Variables in Ruby are not given a type. Their “type” depends upon the value assigned to them. This feature of Ruby is called Dynamic Typing.
In the examples below, the variables PI, a, b, c are not given a type by the programmer. At run time, PI is assigned type float, a and b are assigned type int, and c is assigned type array.
PI = 3.14159 #By convention, constants have uppercase names a = 5 b = 23 c = [1,1,2,3,5,8] #This is an array declaration
Comments in Ruby start with a # and run to the end of the line.
Dynamic typing makes many features like unbounded polymorphism, blocks and closures possible. These features are not available in statically typed languages such as Java and C++.
Everything is an object since Ruby is purely object-oriented. Even numbers, strings and arrays are objects.
5.succ >>6 "changetoupcase".upcase >>CHANGETOUPCASE [3,1,4,2].sort >>[1,2,3,4] “Ruby”.class >>String [1,3,6,8].class >>Array
Here is something we got off the internet which explains Dynamic typing well although its not particular to Ruby.
Arrays
An array in Ruby is an ordered list of elements. An array can be considered an object of objects. In C, C++, Java, C# and other languages, an array consists of elements of a similar type. In Ruby, there is no such constraint. An array can consist of any object. We can have a single array that has Fixnum, String, Array etc as elements.
a = [] # empty array b = [1, 2, 3] # array of FixNums c = ['Hello', 'Ruby'] #array of strings d = ['I am string', 4.5, 1000000, [1,2,3,4]] # elements of different type
Special syntax to create an array of strings without the quotes:
d = %w{Hello Ruby}
Appending elements to an Array is illustrated below:
b << 4 >>[1, 2, 3, 4] b << [10,100] >>[1, 2, 3, 4, [10, 100]]
Array indexes start from zero. Negative indexes count backward. So b[‐1] refers to the last element of b. For example, consider the array b above.
puts b[-1] >>[10, 100] puts b[-2] >>4 puts b[-1][0] >>10 puts b[-1][1] >>100
Arrays can also be created using the new keyword. new is used to create objects of any type.
n = Array.new n << 10 puts n >> 10
Strings
A String in Ruby may be double-quoted ("...") or single-quoted ('...').
However, double-quoting and single-quoting have different effects in some cases. A double-quoted string allows character escapes by a leading backslash, and the evaluation of embedded expressions using #{}. A single-quoted string does not interpret this. This is shown in the example below:
string1 = "abc" >> "abc"
string2 = 'efg' >> "efg"
puts "a\nb\nc" >>a >>b >>c >>nil
puts 'a\nb\n' >>a\nb\nc >>nil
puts "abcd #{5*3} efg" >>"abcd 15 efg"
puts var = " abc " >>" abc " puts "1234#{var}5678" >>"1234 abc 5678"
Ruby strings can be concatenated using + operator. A string can be repeated multiple times using * operator. The example below shows the use of + and * operators. Ruby string handling is more powerful than C. Since Ruby, provides support for regular expressions, string handling and pattern matching can be made very efficiently and easily.
puts "foo" + "bar" >>"foobar"
puts "foo" * 2 >>"foofoo"
Symbols
Symbols in Ruby are Strings, just with an important difference, Symbols are immutable while Strings are not. Mutable objects can be changed after assignment while immutable objects can only be overwritten. Ruby is quite unique in offering mutable Strings, which adds greatly to its expressiveness. However mutable Strings can have their share of issues in terms of creating unexpected results and reduced performance. It is for this reason Ruby also offers programmers the choice of Symbols.
Symbols need not contain only alphanumeric characters, by using quotes you can not only use the same characters as you would in a String, but also interpolate Symbols as well. Syntactically, a Symbol needs a colon : prepended. Symbols are actually so similar to Strings that converting between the two is very simple and consistent.
"hello" :hello
"hello world" :"hello world"
bang = "!"
"hello world#{bang}" # => "hello world!" :"hello world#{bang}" # => :"hello world!"
This and This delve deeply into the difference between string and symbol. Well they are interesting worth reading atleast once.
Hashes
Hash is a collection of Key-Value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. Hashes enumerate their values in the order that the corresponding keys were inserted.
Hashes have a default value that is returned when accessing keys that do not exist in the hash. By default, that value is nil.
Ruby has syntax support for hashes. There are three different ways to create a Hashes.
1. Using the keyword new.
z = Hash.new z["a"] = 'Hello' z["b"] = 'Hi' z["c"] = 'Hey' puts z['a'] >>Hello puts z['b'] >>Hi puts z['c'] >>Hey
2. Using special syntax Hash[ ].
numbers = Hash['one', 1, 'two', 2, 'three', 3, 'four', 4] puts numbers['three'] >>3
3. Using { }
capitals = { "France" => "Paris", "Russia" => "Moscow", "Japan" => "Tokyo", "India" => "New Delhi", "USA" => "Washington, DC" } puts capitals["India"] >>New Delhi
Blocks
Block is just a bunch of code grouped together. They are essentially objects, just like any other in Ruby, i.e They can be passed to other methods and other blocks as values, They take in values depending on the context in which it is used. They are also very useful to make the code more modular.
For example,
def multiply number=5 puts number end => nil multiply =>5
It is a simple function that takes one parameter, multiplies it by a constant, display the result. Now, it looks fine. But, in the case when you want to change the '5' to '6', in this method, one has to locate the function and change it.
But, blocks offer a alternative to that. Yield is used to transfer the control from the method to a 'block' and back to the function again.
def multiply yield(8) end multiply{ |input| puts input } >>8
Here as soon as the execution reaches yield, it is transferred to the block written later, and does the appropriate action.
This example may not be a convincing example. One may argue that the same can be achieved by delegating the task to another method. But blocks are easier because, you don't have to keep track of the other method.
So, you may choose to make the function do,
multiply{ |input| puts input*input*input } >>512
without having to refer back to the original function. The above is a good example of a block. It is a standalone set of code, that associates itself with the method multiply.
Now, it is also possible to use blocks with parametrized functions,
def displayNumber( parameter ) yield( parameter ) end displayNumber(10){ |num| puts num } >>10
The above can be extended to two or more parameters
To compare it with other languages, They are similar to the following,
- Anonymous inner classes in Java
- Anonymous functions in Perl
- Lambdas in Python
- Function objects in C++
- Fucntion pointers in C
It is important to note that, since blocks provide a way to modify the way a method behaves in run-time, it is a very important facet of the polymorphism in Ruby.
Iterators
Iterator is an object which is used to traverse through a collection of data. It pretty much means the same thing as it does in java. But the tricky part is that, unlike java, iterators are defined in the collection and is simply put to use when the particular iterator is used.
There are a few iterators in ruby
- each
- times
- upto and step
- each_index
[1,2,3,4,5].each{ |i| puts i*i } 1 4 9 16 25
The above snippet features a block and an iterator which passes the block to each object in the collection. So, the i*i is passed to all the objects, thus effectively squaring all of them.
5.times{ |i| puts i*i } 1 4 9 16 25
So, this effectively produces the same result as the above, but shall be useful when one has to repeat a task for a n number of times.
Its quite annoying to always start from the number 1 as in the case of Times iterator. One may wish to start from a number other than 1. For such cases there is the upto iterator.
4.upto(10) { |i| puts i*i } 16 25 36 49 64 81 100
The above prints the squares of all numbers from 4 to 10. So basically, 'upto' is 'times' iterator, with a lower bound.
Now in the kinds of situation where we might need to skip a number two as we progress, the 'step' iterator.
2.step(20,2) { |i| puts i*i } 4 16 36 64 100 144 196 256 324 400
The above piece of code, as you might have guessed, displays the square of all even numbers from 2 to 20, inclusive.
Closures
Closure in Ruby is a block that closes over. Assume we have been delegated the task of writing a method that takes a parameter and multiplies it by 6. We would probably go ahead and do the following.
def multByThree ( n ) n*6 end => nil
So far so good. But if we are delegated additional tasks to write multiplier functions to multiply by 1,2,3,4,5...,100 it would be a rather boring and tedious job to create 100 functions to the effect. But ruby has a very elegant solution to it. Closures.
Closure is a function with two additional properties
- It stores the value of all the variables that were in its scope at time of creation.
- It can be passed around as an object.
In layman terms, a closure is a function that returns another function with atleast one less argument.
def multByN ( n ) closure = lambda { |x| n*x } end => nil obj = multByN( 4 ) obj.call 5 => 20
Thus we have created a closure which is C(N,X) = N*X and having passed the parameter 4 on creation, the function F(x) = 4*x is returned by the closure. The lambda in the closure definition refers to a lambda function, which has its roots in functional programming. It basically is used to create functions dynamically which is exactly the purpose of a closure as well.
Currying
Currying is very similar to closures. It is basically used to create a function by fixing some value. But the difference is that, closures create and return lambda functions; while currying takes a normal function, and makes a lambda function out of it. It does something like this.
f(x,y) ==currying==> f'(2,y).
The two was nothing special. The point was to mention that after currying we get back a function with atleast one parameter lesser than the original function. In other words, currying fixes one or more parameters of a function and returns a new function based on the original function.
This is achieved, again by lambda method, just like closures.
def multiply (x,y) val = x*y val end => nil
mulby2 = lambda { |x| multiply(x,2) } mulby3 = lambda { |x| multiply(x,3) }
mulby2.call 2 => 4 mulby3.call 2 => 6
So, one may notice that, currying takes the existing function multiply(x,y) and curries it to form new functions mulby2(x) and mulby3(x). More about currying may be learnt from here. Currying is a very interesting feature and together with closures, it provides a flexibility to ruby at ease which is not easily achievable in java or c++.
Here is a set of nice programming examples in Ruby to explain/implement currying.
Conclusion
Ruby is a purely object oriented language that provides easy programming. The features of Ruby have made it a very popular and extensible language.
See Also
Object oriented Programming
Learn Ruby
Functional programming
Imperative Programming
Interpreted language
Reflection
Dynamic language
Python
Perl
Ruby on Rails
Model View Controller - MVC
Metaprogramming Ruby: Program Like the Ruby Pros
Design Patterns in Ruby
Garbage Collection in Ruby
Threads in Ruby
Ruby interface to C modules
Currying
References
"Programming Ruby - The Pragmatic Programmer's Guide"
ruby-doc
tryruby
Object Oriented Ruby
www.skorks.com
Features of Ruby
Dynamic Typing
Closures
Iterators in Ruby
Mixins
Modules and Mixins
Duck Typing - Unbounded Polymorphism
Metaprogramming
Regular expressions
Interactive Ruby Browser
Array class in Ruby
String
Symbols
Hash class in Ruby
Blocks, Procs and Lambdas