CSC/ECE 517 Fall 2012/Table Of Contents

From Expertiza_Wiki
Revision as of 23:04, 14 November 2012 by Skillam (talk | contribs)
Jump to navigation Jump to search

Ruby

Ruby is a dynamic, reflective, object oriented programming language which was first developed in mid 90s by Yukihiro "Matz" Matsumoto in Japan.[1]. It is a cross plat-form interpreted and object-oriented language. It is designed for the principle of least surprise. Matz says "I wanted to minimize my frustration during programming, so I want to minimize my effort in programming. That was my primary goal in designing Ruby. I want to have fun in programming myself. After releasing Ruby and many people around the world got to know Ruby, they said they feel the way I feel. They came up with the phrase the principle of least surprise."[2] In 2004, web application framework of Ruby "Ruby on Rails" was introduced by David Heinemeier Hansson. With increased complexity of codes, it became essential to design a development environment which will include some intelligence in code writing with increased version control and simpler debugging. Popular IDEs for Ruby are RadRails,Netbeans,Textmate,Rubymine,jRuby and RDT. A brief outline is given on objects,classes and inheritance.Ruby can act as a set-based language and prototype-based language.

A Method is a Subroutine (or Procedure or Function) in a class that defines the behaviour exhibited by the associated Class instances at runtime. Methods defined within a Class are bound to the class either by Static binding or Dynamic binding. An example of a method in Ruby is given here.Instance methods,static methods,accessor methods are different kinds of methods. A closure is basically a method/function that has the following two properties:

  • We can pass it around like an object (to be called later)
  • It remembers the values of all the variables that were in scope<ref>http://en.wikipedia.org/wiki/Scope_(computer_science)</ref> when the function was created. It is then able to access those variables when it is called even though they may no longer be in scope.Closures must be explicitly supported by the language. In order for the language to be able to support closures, it must support first-class functions.

A normal function is defined in a particular scope (i.e. in a class) and can only be called within that scope. This function has access to all the variables in the scope that it is defined, like the parameters that are passed into it as well as class variables. A closure on the other hand may be defined in one scope and be called in a completely different scope (since we can pass it around before calling it). Because of this, when a closure is created, it retains the values of all the variables that were in scope when the closure was defined. Even if the variables are generally no longer in scope when the closure is called, within the closure they still are. In other words, the closure retains knowledge of its lexical environment at the time it was defined. Examples of closures are given here. Instance variables in ruby are defined.Advantages of closures are given here. The use of closures in other languages is given here.

Class variables and class methods in Ruby are defined here.The basic syntax to declare them as class variables and instance variables is also given. Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions.Modules definition is similar to classes just that we use the keyword module instead of the class keyword.Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object. Uses and examples of modules are given here. The most interesting fact about the use of modules is to define mixins. When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods. Mixins are different from #include and multiple inheritance and this is demonstrated here. More about Mixins can be found here.

A method that belongs to a class is called by creating an object of the class and passing the method name to the object as a message. The object then looks up the method lookup path and tries to match the called method with the defined methods in the class. On success, the method is executed and the result is returned. If the object does not find a match in its method lookup, in normal circumstances the NoMethodError Exception is raised . In cases where the user wants to handle the methods which are not defined but are still called, “method_missing” can be defined and the user can handle the methods as he/she sees fit. Here is the format for method missing and Ruby method look up flow. Examples of method missing are given here. Advantages and disadvantages of method missing are given here. Method missing, one of the dynamic features of Ruby, is not a feature that is unique to Ruby. It exists in Smalltalk, Python, Groovy, some Javascripts and most CLOS (Common Lisp Object System)extensions. In this section we look at the few such similar implementations in other languages. The table here gives different ways the functionality related to method_missing is handled in other languages.<ref>http://olabini.com/blog/2010/04/patterns-of-method-missing/</ref> Here are few patterns of method missing.


Namespace in C++ is similar to modulesIn general, a namespace is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.Namespace usually group names based on their functionality. Usage of namespaces and its example is given here. Multiple inheritance in C++ similar to mixins.You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance. An example is given here. Interfaces in JAVA is similar to mixins.A Java interface defines a set of methods but does not implement them.A class that implements the interface agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing multiple inheritance. Properties of an interface are:

  • An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.
  • Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.
  • Methods in an interface are implicitly public.

An example is given over here.Comparison between mixins and interfaces and given here. Comparable and Enumerable are commonly used mixins.

Ruby has simplified the way programmers use loops and iterators. Ruby helps programmers to use Don't Repeat Yourself (DRY) principle effectively by using blocks and defining iterators over collections.<ref name="Iterators">Alan Skorkin. "A wealth of ruby loops and iterators" http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators</ref> This helps in minimizing the development work that programmers often find in any other O-O style programming language. In ruby, these iterators, blocks and functional idioms are explained as:

Iterators are Collection defined methods that helps to iterate over all the elements present in a Collection.<ref name ="tutorial_point_i">"Ruby Iterators" http://www.tutorialspoint.com/ruby/ruby_iterators.htm</ref> Ruby Collections are basically objects that store a group of data members of various types. Examples of Collection are arrays, hashes etc.
A block consists of chunks of codes with a name assigned to it.<ref name="blocks">"Ruby blocks" http://www.tutorialspoint.com/ruby/ruby_blocks.htm</ref> For example,

  my_block { puts "Hello World" }

Different types of iterators in Ruby are times iterator,upto iterator,step iterator,each iterator,collect iterator,map iterator. Ruby allows programmers to define their own iterator other than the iterators listed above. For example if a programmer wishes to define an iterator which prints an array in reverse order, then he can do so as shown here. Blocks are the most commonly used form of closures in Ruby. We can find them all over the core Ruby libraries. They are nameless functions and can be passed anywhere.A Block consists of a chunk of codes with a name assigned to it.Alternately, a block is a code which is passed to the iterator. These blocks can either be defined by enclosing them in curly braces or by using the do...end syntax as shown here. Procs are nameless block of code that can be represented as an object and can be passed around or called at will.Lambdas are a more strict form of proc.

Multiple Inheritance has several disadvantages that can lead to ambiguous code behavior either during compile time or run time.Ruby does not support directMultiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.Given here is the Taggable-string example taken from the Class notes of CSC517, NCSU. Suppose we want to add tags to Strings, we can define a Taggable module and include it into the class. Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways. Advantages and disadvantages of multiple inheritance are given here.


Expression orientation<ref name="saas_video">"SaaS. 3.6 - Ruby Blocks, Iterators, Functional Idioms " http://www.youtube.com/watch?v=bRn91_Zonh4</ref> in ruby refers to the mechanism of applying a series of operation on collections ( e.g. arrays, hashes etc ) without actually modifying the original collection. For example, consider the sort method in ruby. The sort method when called on an array creates a temporary array having the same elements as that of the original array. It then sorts this temporary array and returns that.

Functional idioms refers to the various constructs in ruby that mimics [Functional Programming Language Functional Programming Language. Most programmers argue that Ruby is actually an Object Oriented Programming Language and it does not behaves as per the functional programming paradigm.

Regular expressions are extremely powerful.Rubywas built as a better Perl hence it supports regular expressions.Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. More on regular expressions can be found here.


JRuby is the Ruby Programming Language on the JVM. Ruby is a reflective, dynamic, and interpreted object-oriented scripting language. JRuby is a Java programming language implementation of the Ruby language syntax with all the core libraries plus the standard libraries. With JRuby, you get all of the advantages of Ruby plus access to the full range of Java platform functionality. This can be achieved using the following two flavors.

  • Driving Ruby from Java
  • Driving Java from Ruby

Some examples where this integration can be done are:

  • From a JRuby script, you can call the Java platform Math library to access its powerful computational capabilities or call the Java platform Swing library to display a dialog box that requires end-user input before allowing the script to proceed.
  • You can use the JSR 223 Scripting APIs or the Bean Scripting Framework (BSF) APIs to call a JRuby script from within a Java application to, for example, invoke back-end processing scripts from a servlet to update or generate web content.

More about JRuby is given here. Advantages of JRuby over JAVA and Ruby is given here. Few difficulties of JRuby are addressed here.

Testing approaches must have a logico-mathematical form, i.e., have one right answer. There are different approaches to software testing and are classified into different levels, depending on the stage of Software Development Life cycle (SDLC) in which it is done. The different levels are unit testing,integration testing,system testing,system integration testing and performance testing.

Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit <ref>Ruby Unit Testing Framework</ref>. Other testing frameworks available for Ruby are Shoulda, RSpec and Cucumber. Comparison of Unit Test Frameworks:RSpec,Shoulda and Cucumber is given here. More on testing in Ruby is given here.

Redirection refers to redirection of incoming request from one page to another. HTTP is stateless, hence redirection with session/flash makes it stateful.As the http requests are stateless,it leads to losing the state of the previous request. Thus Rails provides hash called Flash which persist until the end of the next request received and Session hash, which unlike flash persists forever. The working,example,advantages and disadvantages of flash/session is given here.