<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Vsingh3</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Vsingh3"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Vsingh3"/>
	<updated>2026-05-16T17:19:14Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=54389</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=54389"/>
		<updated>2011-10-30T16:08:56Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Duck Typing (Unbounded Polymorphism) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby. &lt;br /&gt;
__TOC__&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Closure_(computer_science) closure] is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.&amp;lt;ref&amp;gt;http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/&amp;lt;/ref&amp;gt;  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from lecture 5 &amp;lt;ref&amp;gt; http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method results in an object that checks the number of its arguments, while a plain Proc.new does not.  Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them&amp;lt;/ref&amp;gt; For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;, we see that instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt;LocalJumpError&amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying] is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) &amp;lt;ref&amp;gt;http://www.khelll.com/blog/ruby/ruby-currying/&amp;lt;/ref&amp;gt;&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
[[File:Ruby.jpg| right| Ruby ]]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Data Encapsulation]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Abstraction_(computer_science) Data Abstraction]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) Inheritance]&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of [http://en.wikipedia.org/wiki/Class_(computer_programming) classes] and [http://en.wikipedia.org/wiki/Object_(object-oriented_programming) objects].  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes &amp;lt;ref&amp;gt;http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object &amp;lt;ref&amp;gt;http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods &amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up &amp;lt;ref&amp;gt;http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Method_(computer_programming)#Abstract_methods &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt;] is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called [http://en.wikipedia.org/wiki/Duck_typing duck typing]. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) &amp;lt;ref&amp;gt;http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502&amp;lt;/ref&amp;gt;&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Duck_typing&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;Person has no feather&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def test_method duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  person = Person.new&lt;br /&gt;
  test_method donald&lt;br /&gt;
  test_method person&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of the above program:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
Person has no feather.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=54388</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=54388"/>
		<updated>2011-10-30T16:08:28Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Duck Typing (Unbounded Polymorphism) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby. &lt;br /&gt;
__TOC__&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Closure_(computer_science) closure] is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.&amp;lt;ref&amp;gt;http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/&amp;lt;/ref&amp;gt;  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from lecture 5 &amp;lt;ref&amp;gt; http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method results in an object that checks the number of its arguments, while a plain Proc.new does not.  Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them&amp;lt;/ref&amp;gt; For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;, we see that instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt;LocalJumpError&amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying] is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) &amp;lt;ref&amp;gt;http://www.khelll.com/blog/ruby/ruby-currying/&amp;lt;/ref&amp;gt;&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
[[File:Ruby.jpg| right| Ruby ]]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Data Encapsulation]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Abstraction_(computer_science) Data Abstraction]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) Inheritance]&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of [http://en.wikipedia.org/wiki/Class_(computer_programming) classes] and [http://en.wikipedia.org/wiki/Object_(object-oriented_programming) objects].  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes &amp;lt;ref&amp;gt;http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object &amp;lt;ref&amp;gt;http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods &amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up &amp;lt;ref&amp;gt;http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Method_(computer_programming)#Abstract_methods &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt;] is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called [http://en.wikipedia.org/wiki/Duck_typing duck typing]. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) &amp;lt;ref&amp;gt;http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502&amp;lt;/ref&amp;gt;&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Duck_typing&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;Person has no feather&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def test_method duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  person = Person.new&lt;br /&gt;
  duck_features donald&lt;br /&gt;
  duck_features person&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of the above program:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
Person has no feather.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=54142</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=54142"/>
		<updated>2011-10-23T23:35:56Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Duck Typing (Unbounded Polymorphism) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby. &lt;br /&gt;
__TOC__&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Closure_(computer_science) closure] is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.&amp;lt;ref&amp;gt;http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/&amp;lt;/ref&amp;gt;  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from lecture 5 &amp;lt;ref&amp;gt; http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method results in an object that checks the number of its arguments, while a plain Proc.new does not.  Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them&amp;lt;/ref&amp;gt; For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;, we see that instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt;LocalJumpError&amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying] is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) &amp;lt;ref&amp;gt;http://www.khelll.com/blog/ruby/ruby-currying/&amp;lt;/ref&amp;gt;&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
[[File:Ruby.jpg| right| Ruby ]]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Data Encapsulation]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Abstraction_(computer_science) Data Abstraction]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) Inheritance]&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of [http://en.wikipedia.org/wiki/Class_(computer_programming) classes] and [http://en.wikipedia.org/wiki/Object_(object-oriented_programming) objects].  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes &amp;lt;ref&amp;gt;http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object &amp;lt;ref&amp;gt;http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods &amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up &amp;lt;ref&amp;gt;http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Method_(computer_programming)#Abstract_methods &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt;] is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called [http://en.wikipedia.org/wiki/Duck_typing duck typing]. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) &amp;lt;ref&amp;gt;http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502&amp;lt;/ref&amp;gt;&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Duck_typing&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;Person has no feather&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  per = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest per&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of the above program:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
Person has no feather.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=54141</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=54141"/>
		<updated>2011-10-23T23:34:20Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Abstract Methods */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby. &lt;br /&gt;
__TOC__&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Closure_(computer_science) closure] is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.&amp;lt;ref&amp;gt;http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/&amp;lt;/ref&amp;gt;  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from lecture 5 &amp;lt;ref&amp;gt; http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method results in an object that checks the number of its arguments, while a plain Proc.new does not.  Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them&amp;lt;/ref&amp;gt; For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;, we see that instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt;LocalJumpError&amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying] is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) &amp;lt;ref&amp;gt;http://www.khelll.com/blog/ruby/ruby-currying/&amp;lt;/ref&amp;gt;&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
[[File:Ruby.jpg| right| Ruby ]]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Data Encapsulation]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Abstraction_(computer_science) Data Abstraction]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) Inheritance]&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of [http://en.wikipedia.org/wiki/Class_(computer_programming) classes] and [http://en.wikipedia.org/wiki/Object_(object-oriented_programming) objects].  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes &amp;lt;ref&amp;gt;http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object &amp;lt;ref&amp;gt;http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods &amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up &amp;lt;ref&amp;gt;http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Method_(computer_programming)#Abstract_methods &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt;] is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) &amp;lt;ref&amp;gt;http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502&amp;lt;/ref&amp;gt;&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Duck_typing&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;Person has no feather&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  per = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest per&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of the above program:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
Person has no feather.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=54140</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=54140"/>
		<updated>2011-10-23T23:27:38Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Object-Oriented Programming in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby. &lt;br /&gt;
__TOC__&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Closure_(computer_science) closure] is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.&amp;lt;ref&amp;gt;http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/&amp;lt;/ref&amp;gt;  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from lecture 5 &amp;lt;ref&amp;gt; http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method results in an object that checks the number of its arguments, while a plain Proc.new does not.  Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them&amp;lt;/ref&amp;gt; For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;, we see that instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt;LocalJumpError&amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying] is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) &amp;lt;ref&amp;gt;http://www.khelll.com/blog/ruby/ruby-currying/&amp;lt;/ref&amp;gt;&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
[[File:Ruby.jpg| right| Ruby ]]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Data Encapsulation]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Abstraction_(computer_science) Data Abstraction]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) Inheritance]&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of [http://en.wikipedia.org/wiki/Class_(computer_programming) classes] and [http://en.wikipedia.org/wiki/Object_(object-oriented_programming) objects].  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes &amp;lt;ref&amp;gt;http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object &amp;lt;ref&amp;gt;http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods &amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up &amp;lt;ref&amp;gt;http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) &amp;lt;ref&amp;gt;http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502&amp;lt;/ref&amp;gt;&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Duck_typing&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;Person has no feather&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  per = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest per&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of the above program:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
Person has no feather.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=54139</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=54139"/>
		<updated>2011-10-23T23:26:14Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Object-Oriented Programming in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby. &lt;br /&gt;
__TOC__&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Closure_(computer_science) closure] is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.&amp;lt;ref&amp;gt;http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/&amp;lt;/ref&amp;gt;  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from lecture 5 &amp;lt;ref&amp;gt; http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method results in an object that checks the number of its arguments, while a plain Proc.new does not.  Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them&amp;lt;/ref&amp;gt; For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;, we see that instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt;LocalJumpError&amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying] is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) &amp;lt;ref&amp;gt;http://www.khelll.com/blog/ruby/ruby-currying/&amp;lt;/ref&amp;gt;&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
[[File:Ruby.jpg| right| Ruby ]]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Data Encapsulation]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Abstraction_(computer_science) Data Abstraction]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) Inheritance]&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes &amp;lt;ref&amp;gt;http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object &amp;lt;ref&amp;gt;http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods &amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up &amp;lt;ref&amp;gt;http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) &amp;lt;ref&amp;gt;http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502&amp;lt;/ref&amp;gt;&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Duck_typing&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;Person has no feather&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  per = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest per&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of the above program:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
Person has no feather.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=54138</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=54138"/>
		<updated>2011-10-23T23:24:09Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Currying */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby. &lt;br /&gt;
__TOC__&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Closure_(computer_science) closure] is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.&amp;lt;ref&amp;gt;http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/&amp;lt;/ref&amp;gt;  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from lecture 5 &amp;lt;ref&amp;gt; http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method results in an object that checks the number of its arguments, while a plain Proc.new does not.  Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them&amp;lt;/ref&amp;gt; For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;, we see that instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt;LocalJumpError&amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying] is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) &amp;lt;ref&amp;gt;http://www.khelll.com/blog/ruby/ruby-currying/&amp;lt;/ref&amp;gt;&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
[[File:Ruby.jpg| right| Ruby ]]&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes &amp;lt;ref&amp;gt;http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object &amp;lt;ref&amp;gt;http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods &amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up &amp;lt;ref&amp;gt;http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) &amp;lt;ref&amp;gt;http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502&amp;lt;/ref&amp;gt;&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Duck_typing&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;Person has no feather&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  per = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest per&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of the above program:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
Person has no feather.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=54137</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=54137"/>
		<updated>2011-10-23T23:22:31Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Closures */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby. &lt;br /&gt;
__TOC__&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Closure_(computer_science) closure] is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.&amp;lt;ref&amp;gt;http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/&amp;lt;/ref&amp;gt;  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from lecture 5 &amp;lt;ref&amp;gt; http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method results in an object that checks the number of its arguments, while a plain Proc.new does not.  Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them&amp;lt;/ref&amp;gt; For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;, we see that instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt;LocalJumpError&amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) &amp;lt;ref&amp;gt;http://www.khelll.com/blog/ruby/ruby-currying/&amp;lt;/ref&amp;gt;&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
[[File:Ruby.jpg| right| Ruby ]]&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes &amp;lt;ref&amp;gt;http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object &amp;lt;ref&amp;gt;http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods &amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up &amp;lt;ref&amp;gt;http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) &amp;lt;ref&amp;gt;http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502&amp;lt;/ref&amp;gt;&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Duck_typing&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;Person has no feather&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  per = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest per&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of the above program:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
Person has no feather.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53426</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53426"/>
		<updated>2011-10-20T23:58:48Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Duck Typing (Unbounded Polymorphism) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby. &lt;br /&gt;
__TOC__&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.&amp;lt;ref&amp;gt;http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/&amp;lt;/ref&amp;gt;  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from lecture 5 &amp;lt;ref&amp;gt; http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them&amp;lt;/ref&amp;gt; For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) &amp;lt;ref&amp;gt;http://www.khelll.com/blog/ruby/ruby-currying/&amp;lt;/ref&amp;gt;&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
[[File:Ruby.jpg| right| Ruby ]]&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes &amp;lt;ref&amp;gt;http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object &amp;lt;ref&amp;gt;http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods &amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up &amp;lt;ref&amp;gt;http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) &amp;lt;ref&amp;gt;http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502&amp;lt;/ref&amp;gt;&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Duck_typing&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;Person has no feather&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  per = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest per&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of the above program:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
Person has no feather.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53424</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53424"/>
		<updated>2011-10-20T23:58:22Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Duck Typing (Unbounded Polymorphism) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby. &lt;br /&gt;
__TOC__&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.&amp;lt;ref&amp;gt;http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/&amp;lt;/ref&amp;gt;  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from lecture 5 &amp;lt;ref&amp;gt; http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them&amp;lt;/ref&amp;gt; For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) &amp;lt;ref&amp;gt;http://www.khelll.com/blog/ruby/ruby-currying/&amp;lt;/ref&amp;gt;&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
[[File:Ruby.jpg| right| Ruby ]]&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes &amp;lt;ref&amp;gt;http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object &amp;lt;ref&amp;gt;http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods &amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up &amp;lt;ref&amp;gt;http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) &amp;lt;ref&amp;gt;http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502&amp;lt;/ref&amp;gt;&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Duck_typing&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;Person has no feather&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  per = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest per&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of the above program:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
Person has no feather.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53297</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53297"/>
		<updated>2011-10-20T22:27:36Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Duck Typing (Unbounded Polymorphism) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby. &lt;br /&gt;
__TOC__&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.&amp;lt;ref&amp;gt;http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/&amp;lt;/ref&amp;gt;  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from lecture 5 &amp;lt;ref&amp;gt; http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them&amp;lt;/ref&amp;gt; For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) &amp;lt;ref&amp;gt;http://www.khelll.com/blog/ruby/ruby-currying/&amp;lt;/ref&amp;gt;&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
[[File:Ruby.jpg| right| Ruby ]]&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes &amp;lt;ref&amp;gt;http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object &amp;lt;ref&amp;gt;http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods &amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up &amp;lt;ref&amp;gt;http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) &amp;lt;ref&amp;gt;http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502&amp;lt;/ref&amp;gt;&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Duck_typing&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has white and gray feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The person takes a feather from the ground and shows it.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  john = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest john&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of the above program:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has white and gray feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
The person takes a feather from the ground and shows it.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53293</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53293"/>
		<updated>2011-10-20T22:24:14Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Duck Typing (Unbounded Polymorphism) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby. &lt;br /&gt;
__TOC__&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.&amp;lt;ref&amp;gt;http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/&amp;lt;/ref&amp;gt;  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from lecture 5 &amp;lt;ref&amp;gt; http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them&amp;lt;/ref&amp;gt; For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) &amp;lt;ref&amp;gt;http://www.khelll.com/blog/ruby/ruby-currying/&amp;lt;/ref&amp;gt;&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
[[File:Ruby.jpg| right| Ruby ]]&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes &amp;lt;ref&amp;gt;http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object &amp;lt;ref&amp;gt;http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods &amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up &amp;lt;ref&amp;gt;http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies [http://en.wikipedia.org/wiki/Duck_typing]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has white and gray feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The person takes a feather from the ground and shows it.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  john = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest john&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of the above program:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has white and gray feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
The person takes a feather from the ground and shows it.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53291</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53291"/>
		<updated>2011-10-20T22:22:40Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Duck Typing (Unbounded Polymorphism) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby. &lt;br /&gt;
__TOC__&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.&amp;lt;ref&amp;gt;http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/&amp;lt;/ref&amp;gt;  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from lecture 5 &amp;lt;ref&amp;gt; http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them&amp;lt;/ref&amp;gt; For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) &amp;lt;ref&amp;gt;http://www.khelll.com/blog/ruby/ruby-currying/&amp;lt;/ref&amp;gt;&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
[[File:Ruby.jpg| right| Ruby ]]&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes &amp;lt;ref&amp;gt;http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object &amp;lt;ref&amp;gt;http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods &amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up &amp;lt;ref&amp;gt;http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [4]&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies.[5]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has white and gray feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The person takes a feather from the ground and shows it.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  john = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest john&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output of the above program:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has white and gray feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
The person takes a feather from the ground and shows it.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53290</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53290"/>
		<updated>2011-10-20T22:20:39Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: Undo revision 53285 by Vsingh3 (talk)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby. &lt;br /&gt;
__TOC__&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.&amp;lt;ref&amp;gt;http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/&amp;lt;/ref&amp;gt;  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from lecture 5 &amp;lt;ref&amp;gt; http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them&amp;lt;/ref&amp;gt; For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) &amp;lt;ref&amp;gt;http://www.khelll.com/blog/ruby/ruby-currying/&amp;lt;/ref&amp;gt;&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
[[File:Ruby.jpg| right| Ruby ]]&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes &amp;lt;ref&amp;gt;http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object &amp;lt;ref&amp;gt;http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods &amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up &amp;lt;ref&amp;gt;http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) &amp;lt;ref&amp;gt; http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53285</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53285"/>
		<updated>2011-10-20T22:18:00Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ (7)]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them (1)].  For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes.&lt;br /&gt;
&lt;br /&gt;
Ruby provides some convenient method to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut. If you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object. &lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute with method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies [http://en.wikipedia.org/wiki/Duck_typing]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has white and gray feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The person takes a feather from the ground and shows it.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  john = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest john&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Output of the above program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has white and gray feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
The person takes a feather from the ground and shows it.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
*[http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them (1)http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them]&lt;br /&gt;
*[http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby (2)http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Closure_(computer_science) (3)http://en.wikipedia.org/wiki/Closure_(computer_science)]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Continuation-passing_style (4)http://en.wikipedia.org/wiki/Continuation-passing_style]&lt;br /&gt;
*[http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177/ref=sr_1_1?ie=UTF8&amp;amp;qid=1318716890&amp;amp;sr=8-1 (5)Flanagan, D and Matsumoto, Y, (2008). The Ruby Programming Language, O’Reilly Media, Inc.,]&lt;br /&gt;
*[http://www.amazon.com/Learning-Ruby-Michael-James-Fitzgerald/dp/0596529864/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1318716919&amp;amp;sr=1-1 (6)Fitzgerald Michael, (2007). Learning Ruby, O’Reilly Media, Inc., (example)]&lt;br /&gt;
*[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ (7)http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/]&lt;br /&gt;
*[http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/ (8)http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/]&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53264</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53264"/>
		<updated>2011-10-20T22:11:10Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Lecture 5 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby. &lt;br /&gt;
__TOC__&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.&amp;lt;ref&amp;gt;http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/&amp;lt;/ref&amp;gt;  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from lecture 5 &amp;lt;ref&amp;gt; http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
[[File:Ruby.jpg| right| Ruby ]]&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) &amp;lt;ref&amp;gt; http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53261</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53261"/>
		<updated>2011-10-20T22:10:31Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Lecture 5 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby. [[File:Ruby.jpg| right| Ruby ]]&lt;br /&gt;
__TOC__&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) &amp;lt;ref&amp;gt; http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53260</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53260"/>
		<updated>2011-10-20T22:10:12Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Lecture 5 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby.&lt;br /&gt;
__TOC__&lt;br /&gt;
[[File:Ruby.jpg| right| Ruby ]]&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) &amp;lt;ref&amp;gt; http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53259</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53259"/>
		<updated>2011-10-20T22:09:55Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* What is a Closure? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) &amp;lt;ref&amp;gt; http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53258</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53258"/>
		<updated>2011-10-20T22:09:40Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Lecture 5 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
[[File:Ruby.jpg| right| Ruby ]]&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) &amp;lt;ref&amp;gt; http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53255</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53255"/>
		<updated>2011-10-20T22:09:02Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Object-Oriented Programming in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) &amp;lt;ref&amp;gt; http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53253</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53253"/>
		<updated>2011-10-20T22:08:43Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Object-Oriented Programming in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
[[File:Ruby.jpg| right| Ruby ]]&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties&lt;br /&gt;
&lt;br /&gt;
{{Reflist}}&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53251</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53251"/>
		<updated>2011-10-20T22:07:59Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Object-Oriented Programming in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
[File:Ruby.jpg| right]&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties&lt;br /&gt;
&lt;br /&gt;
{{Reflist}}&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Ruby.jpg&amp;diff=53250</id>
		<title>File:Ruby.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Ruby.jpg&amp;diff=53250"/>
		<updated>2011-10-20T22:07:04Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: uploaded a new version of &amp;amp;quot;File:Ruby.jpg&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53247</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53247"/>
		<updated>2011-10-20T22:05:54Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Lecture 5 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties&lt;br /&gt;
&lt;br /&gt;
{{Reflist}}&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53246</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53246"/>
		<updated>2011-10-20T22:05:45Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Lecture 5 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
This wiki covers Review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties&lt;br /&gt;
&lt;br /&gt;
{{Reflist}}&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53245</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53245"/>
		<updated>2011-10-20T22:05:28Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Lecture 5 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
4b. Lecture 5: This wiki covers Review topics from lecture 4 i.e. Closures and Currying followed by OOP in Ruby.&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties&lt;br /&gt;
&lt;br /&gt;
{{Reflist}}&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53241</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53241"/>
		<updated>2011-10-20T22:03:33Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53234</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53234"/>
		<updated>2011-10-20T22:00:42Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53229</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53229"/>
		<updated>2011-10-20T21:58:55Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
&lt;br /&gt;
_TOC_&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53225</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=53225"/>
		<updated>2011-10-20T21:52:56Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Lecture 5=&lt;br /&gt;
== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52970</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52970"/>
		<updated>2011-10-20T06:27:25Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies [http://en.wikipedia.org/wiki/Duck_typing]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has white and gray feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The person takes a feather from the ground and shows it.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  john = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest john&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Output of the above program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has white and gray feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
The person takes a feather from the ground and shows it.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
#[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/#.Tp-9lOssobU Why ruby? part two – blocks and closures]&lt;br /&gt;
#[http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them]&lt;br /&gt;
#[http://www.khelll.com/blog/ruby/ruby-currying/ Ruby Currying]&lt;br /&gt;
#[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html Classes, Objects, and Variables]&lt;br /&gt;
#[http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ Programming Ruby: the pragmatic programmer's guide by David Thomas, Andrew Hunt]&lt;br /&gt;
#[http://rubylearning.com/satishtalim/ruby_access_control.html Ruby access controls]&lt;br /&gt;
#[http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/ Access Control in Ruby]&lt;br /&gt;
#[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502 Duck typing right?]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Duck_typing Duck typing wiki]&lt;br /&gt;
#[http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Closure_(computer_science) http://en.wikipedia.org/wiki/Closure_(computer_science)]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Continuation-passing_style http://en.wikipedia.org/wiki/Continuation-passing_style]&lt;br /&gt;
#[http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177/ref=sr_1_1?ie=UTF8&amp;amp;qid=1318716890&amp;amp;sr=8-1 Flanagan, D and Matsumoto, Y, (2008). The Ruby Programming Language, O’Reilly Media, Inc.,]&lt;br /&gt;
#[http://www.amazon.com/Learning-Ruby-Michael-James-Fitzgerald/dp/0596529864/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1318716919&amp;amp;sr=1-1 Fitzgerald Michael, (2007). Learning Ruby, O’Reilly Media, Inc., (example)]&lt;br /&gt;
#[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/]&lt;br /&gt;
#[http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/ http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/]&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52969</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52969"/>
		<updated>2011-10-20T06:26:18Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies [http://en.wikipedia.org/wiki/Duck_typing]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has white and gray feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The person takes a feather from the ground and shows it.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  john = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest john&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Output of the above program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has white and gray feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
The person takes a feather from the ground and shows it.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
#[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/#.Tp-9lOssobU Why ruby? part two – blocks and closures]&lt;br /&gt;
#[http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them]&lt;br /&gt;
#[http://www.khelll.com/blog/ruby/ruby-currying/ Ruby Currying]&lt;br /&gt;
#[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html Classes, Objects, and Variables]&lt;br /&gt;
#[http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ Programming Ruby: the pragmatic programmer's guide by David Thomas, Andrew Hunt]&lt;br /&gt;
#[http://rubylearning.com/satishtalim/ruby_access_control.html Ruby access controls]&lt;br /&gt;
#[http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/ Access Control in Ruby]&lt;br /&gt;
#[http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Closure_(computer_science) http://en.wikipedia.org/wiki/Closure_(computer_science)]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Continuation-passing_style http://en.wikipedia.org/wiki/Continuation-passing_style]&lt;br /&gt;
#[http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177/ref=sr_1_1?ie=UTF8&amp;amp;qid=1318716890&amp;amp;sr=8-1 Flanagan, D and Matsumoto, Y, (2008). The Ruby Programming Language, O’Reilly Media, Inc.,]&lt;br /&gt;
#[http://www.amazon.com/Learning-Ruby-Michael-James-Fitzgerald/dp/0596529864/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1318716919&amp;amp;sr=1-1 Fitzgerald Michael, (2007). Learning Ruby, O’Reilly Media, Inc., (example)]&lt;br /&gt;
#[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/]&lt;br /&gt;
#[http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/ http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/]&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52968</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52968"/>
		<updated>2011-10-20T06:24:39Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies [http://en.wikipedia.org/wiki/Duck_typing]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has white and gray feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The person takes a feather from the ground and shows it.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  john = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest john&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Output of the above program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has white and gray feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
The person takes a feather from the ground and shows it.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
#[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/#.Tp-9lOssobU Why ruby? part two – blocks and closures]&lt;br /&gt;
#[http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them]&lt;br /&gt;
#[http://www.khelll.com/blog/ruby/ruby-currying/ Ruby Currying]&lt;br /&gt;
#[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html Classes, Objects, and Variables]&lt;br /&gt;
#[http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ Programming Ruby: the pragmatic programmer's guide by David Thomas, Andrew Hunt]&lt;br /&gt;
#[http://rubylearning.com/satishtalim/ruby_access_control.html Ruby access controls]&lt;br /&gt;
#[http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Closure_(computer_science) http://en.wikipedia.org/wiki/Closure_(computer_science)]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Continuation-passing_style http://en.wikipedia.org/wiki/Continuation-passing_style]&lt;br /&gt;
#[http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177/ref=sr_1_1?ie=UTF8&amp;amp;qid=1318716890&amp;amp;sr=8-1 Flanagan, D and Matsumoto, Y, (2008). The Ruby Programming Language, O’Reilly Media, Inc.,]&lt;br /&gt;
#[http://www.amazon.com/Learning-Ruby-Michael-James-Fitzgerald/dp/0596529864/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1318716919&amp;amp;sr=1-1 Fitzgerald Michael, (2007). Learning Ruby, O’Reilly Media, Inc., (example)]&lt;br /&gt;
#[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/]&lt;br /&gt;
#[http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/ http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/]&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52967</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52967"/>
		<updated>2011-10-20T06:23:41Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies [http://en.wikipedia.org/wiki/Duck_typing]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has white and gray feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The person takes a feather from the ground and shows it.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  john = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest john&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Output of the above program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has white and gray feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
The person takes a feather from the ground and shows it.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
#[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/#.Tp-9lOssobU Why ruby? part two – blocks and closures]&lt;br /&gt;
#[http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them]&lt;br /&gt;
#[http://www.khelll.com/blog/ruby/ruby-currying/ Ruby Currying]&lt;br /&gt;
#[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html Classes, Objects, and Variables]&lt;br /&gt;
#[http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ Programming Ruby: the pragmatic programmer's guide by David Thomas, Andrew Hunt]&lt;br /&gt;
#[http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Closure_(computer_science) http://en.wikipedia.org/wiki/Closure_(computer_science)]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Continuation-passing_style http://en.wikipedia.org/wiki/Continuation-passing_style]&lt;br /&gt;
#[http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177/ref=sr_1_1?ie=UTF8&amp;amp;qid=1318716890&amp;amp;sr=8-1 Flanagan, D and Matsumoto, Y, (2008). The Ruby Programming Language, O’Reilly Media, Inc.,]&lt;br /&gt;
#[http://www.amazon.com/Learning-Ruby-Michael-James-Fitzgerald/dp/0596529864/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1318716919&amp;amp;sr=1-1 Fitzgerald Michael, (2007). Learning Ruby, O’Reilly Media, Inc., (example)]&lt;br /&gt;
#[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/]&lt;br /&gt;
#[http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/ http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/]&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52965</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52965"/>
		<updated>2011-10-20T06:22:48Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies [http://en.wikipedia.org/wiki/Duck_typing]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has white and gray feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The person takes a feather from the ground and shows it.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  john = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest john&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Output of the above program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has white and gray feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
The person takes a feather from the ground and shows it.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
#[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/#.Tp-9lOssobU Why ruby? part two – blocks and closures]&lt;br /&gt;
#[http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them]&lt;br /&gt;
#[http://www.khelll.com/blog/ruby/ruby-currying/ Ruby Currying]&lt;br /&gt;
#[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html Classes, Objects, and Variables]&lt;br /&gt;
#[http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Closure_(computer_science) http://en.wikipedia.org/wiki/Closure_(computer_science)]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Continuation-passing_style http://en.wikipedia.org/wiki/Continuation-passing_style]&lt;br /&gt;
#[http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177/ref=sr_1_1?ie=UTF8&amp;amp;qid=1318716890&amp;amp;sr=8-1 Flanagan, D and Matsumoto, Y, (2008). The Ruby Programming Language, O’Reilly Media, Inc.,]&lt;br /&gt;
#[http://www.amazon.com/Learning-Ruby-Michael-James-Fitzgerald/dp/0596529864/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1318716919&amp;amp;sr=1-1 Fitzgerald Michael, (2007). Learning Ruby, O’Reilly Media, Inc., (example)]&lt;br /&gt;
#[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/]&lt;br /&gt;
#[http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/ http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/]&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52963</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52963"/>
		<updated>2011-10-20T06:22:06Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies [http://en.wikipedia.org/wiki/Duck_typing]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has white and gray feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The person takes a feather from the ground and shows it.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  john = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest john&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Output of the above program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has white and gray feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
The person takes a feather from the ground and shows it.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
#[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/#.Tp-9lOssobU Why ruby? part two – blocks and closures]&lt;br /&gt;
#[http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them]&lt;br /&gt;
#[http://www.khelll.com/blog/ruby/ruby-currying/ Ruby Currying]&lt;br /&gt;
#[http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Closure_(computer_science) http://en.wikipedia.org/wiki/Closure_(computer_science)]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Continuation-passing_style http://en.wikipedia.org/wiki/Continuation-passing_style]&lt;br /&gt;
#[http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177/ref=sr_1_1?ie=UTF8&amp;amp;qid=1318716890&amp;amp;sr=8-1 Flanagan, D and Matsumoto, Y, (2008). The Ruby Programming Language, O’Reilly Media, Inc.,]&lt;br /&gt;
#[http://www.amazon.com/Learning-Ruby-Michael-James-Fitzgerald/dp/0596529864/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1318716919&amp;amp;sr=1-1 Fitzgerald Michael, (2007). Learning Ruby, O’Reilly Media, Inc., (example)]&lt;br /&gt;
#[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/]&lt;br /&gt;
#[http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/ http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/]&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52962</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52962"/>
		<updated>2011-10-20T06:20:40Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies [http://en.wikipedia.org/wiki/Duck_typing]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has white and gray feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The person takes a feather from the ground and shows it.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  john = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest john&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Output of the above program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has white and gray feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
The person takes a feather from the ground and shows it.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
#[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/#.Tp-9lOssobU Why ruby? part two – blocks and closures]&lt;br /&gt;
#[http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them]&lt;br /&gt;
#[http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Closure_(computer_science) http://en.wikipedia.org/wiki/Closure_(computer_science)]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Continuation-passing_style http://en.wikipedia.org/wiki/Continuation-passing_style]&lt;br /&gt;
#[http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177/ref=sr_1_1?ie=UTF8&amp;amp;qid=1318716890&amp;amp;sr=8-1 Flanagan, D and Matsumoto, Y, (2008). The Ruby Programming Language, O’Reilly Media, Inc.,]&lt;br /&gt;
#[http://www.amazon.com/Learning-Ruby-Michael-James-Fitzgerald/dp/0596529864/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1318716919&amp;amp;sr=1-1 Fitzgerald Michael, (2007). Learning Ruby, O’Reilly Media, Inc., (example)]&lt;br /&gt;
#[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/]&lt;br /&gt;
#[http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/ http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/]&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52961</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52961"/>
		<updated>2011-10-20T06:20:15Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&amp;lt;b&amp;gt; Inheritance &amp;lt;/b&amp;gt; is one of the basic principles of Object-Oriented programming, where Inheritance is a method of altering or reusing code of existing objects.  The inherited class is called the super, or base, class, and the class that inherits from the base class is referred to as the subclass.  Simply, the base class is commonly the more generic form of the subclass.  In Ruby, inheritance is indicated by use of the &amp;lt;b&amp;gt;&amp;lt;&amp;lt;/b&amp;gt; operator.  Using our &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class from above, we will define an &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class which will inherit from the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   def initialize(fname, lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass, &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we added an instance variable called &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  Within the subclass's &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method, the &amp;lt;b&amp;gt;initialize&amp;lt;/b&amp;gt; method of the super class is called first using &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; and passing it the arguments &amp;lt;b&amp;gt;fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lname&amp;lt;/b&amp;gt;, then sets the &amp;lt;b&amp;gt;salary&amp;lt;/b&amp;gt;.  By redefining the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt; class, we have overridden the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; that was defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class.  The &amp;lt;b&amp;gt;super&amp;lt;/b&amp;gt; within the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method calls the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; of the superclass, the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;class, which returns a string.  So, the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; method concatenates the string returned from calling the &amp;lt;b&amp;gt;to_s&amp;lt;/b&amp;gt; from the superclass with the additional salary information.&lt;br /&gt;
&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
An &amp;lt;b&amp;gt;Abstract Method&amp;lt;/b&amp;gt; is a method that has a signature, but it does not have an implementation and are commonly used to specify intefaces.  Abstract methods force the subclass to provide the implementation of the method.  Let's add an abstract method called &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt; to the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt; class. In the &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, we want to print the person's first, middle, and last name, however, the middle name variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is not defined in the &amp;lt;b&amp;gt;Person&amp;lt;/b&amp;gt;, so it must be defined in the superclass to avoid an implementation error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def fullname&lt;br /&gt;
      &amp;quot;Name: #{@fname} #{@mname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the subclass &amp;lt;b&amp;gt;Employee&amp;lt;/b&amp;gt;, we have added the instance variable &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; along with the read/write methods for &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt;.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Employee &amp;lt; Person&lt;br /&gt;
   attr_accessor :salary&lt;br /&gt;
   attr_accessor :mname&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname, mname,lname, salary)&lt;br /&gt;
      super(fname, lname)&lt;br /&gt;
      @salary = salary&lt;br /&gt;
      @mname  = mname&lt;br /&gt;
   end&lt;br /&gt;
   def to_s&lt;br /&gt;
      super + &amp;quot; Salary: #{@salary}&amp;quot;&lt;br /&gt;
   end   &lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since, &amp;lt;b&amp;gt;mname&amp;lt;/b&amp;gt; is now defined, when we call the method &amp;lt;b&amp;gt;fullname&amp;lt;/b&amp;gt;, it prints the Employee's first, middle, and last name:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Employee.new(&amp;quot;Jimmy&amp;quot;,&amp;quot;James&amp;quot;,&amp;quot;Johnson&amp;quot;,35000).fullname&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; Name: Jimmy James Johnson&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies [http://en.wikipedia.org/wiki/Duck_typing]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has white and gray feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The person takes a feather from the ground and shows it.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  john = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest john&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Output of the above program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has white and gray feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
The person takes a feather from the ground and shows it.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
#[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/#.Tp-9lOssobU]&lt;br /&gt;
#[http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them]&lt;br /&gt;
#[http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Closure_(computer_science) http://en.wikipedia.org/wiki/Closure_(computer_science)]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Continuation-passing_style http://en.wikipedia.org/wiki/Continuation-passing_style]&lt;br /&gt;
#[http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177/ref=sr_1_1?ie=UTF8&amp;amp;qid=1318716890&amp;amp;sr=8-1 Flanagan, D and Matsumoto, Y, (2008). The Ruby Programming Language, O’Reilly Media, Inc.,]&lt;br /&gt;
#[http://www.amazon.com/Learning-Ruby-Michael-James-Fitzgerald/dp/0596529864/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1318716919&amp;amp;sr=1-1 Fitzgerald Michael, (2007). Learning Ruby, O’Reilly Media, Inc., (example)]&lt;br /&gt;
#[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/]&lt;br /&gt;
#[http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/ http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/]&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52651</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52651"/>
		<updated>2011-10-19T15:52:08Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* procs vs. lambdas */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them] For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies [http://en.wikipedia.org/wiki/Duck_typing]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has white and gray feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The person takes a feather from the ground and shows it.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  john = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest john&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Output of the above program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has white and gray feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
The person takes a feather from the ground and shows it.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#[http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them]&lt;br /&gt;
#[http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Closure_(computer_science) http://en.wikipedia.org/wiki/Closure_(computer_science)]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Continuation-passing_style http://en.wikipedia.org/wiki/Continuation-passing_style]&lt;br /&gt;
#[http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177/ref=sr_1_1?ie=UTF8&amp;amp;qid=1318716890&amp;amp;sr=8-1 Flanagan, D and Matsumoto, Y, (2008). The Ruby Programming Language, O’Reilly Media, Inc.,]&lt;br /&gt;
#[http://www.amazon.com/Learning-Ruby-Michael-James-Fitzgerald/dp/0596529864/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1318716919&amp;amp;sr=1-1 Fitzgerald Michael, (2007). Learning Ruby, O’Reilly Media, Inc., (example)]&lt;br /&gt;
#[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/]&lt;br /&gt;
#[http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/ http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/]&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52650</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52650"/>
		<updated>2011-10-19T15:51:57Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* procs vs. lambdas */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ ]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them].  For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies [http://en.wikipedia.org/wiki/Duck_typing]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has white and gray feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The person takes a feather from the ground and shows it.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  john = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest john&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Output of the above program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has white and gray feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
The person takes a feather from the ground and shows it.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#[http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them]&lt;br /&gt;
#[http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Closure_(computer_science) http://en.wikipedia.org/wiki/Closure_(computer_science)]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Continuation-passing_style http://en.wikipedia.org/wiki/Continuation-passing_style]&lt;br /&gt;
#[http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177/ref=sr_1_1?ie=UTF8&amp;amp;qid=1318716890&amp;amp;sr=8-1 Flanagan, D and Matsumoto, Y, (2008). The Ruby Programming Language, O’Reilly Media, Inc.,]&lt;br /&gt;
#[http://www.amazon.com/Learning-Ruby-Michael-James-Fitzgerald/dp/0596529864/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1318716919&amp;amp;sr=1-1 Fitzgerald Michael, (2007). Learning Ruby, O’Reilly Media, Inc., (example)]&lt;br /&gt;
#[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/]&lt;br /&gt;
#[http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/ http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/]&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52603</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52603"/>
		<updated>2011-10-19T14:14:48Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Currying */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ (7)]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them (1)].  For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies [http://en.wikipedia.org/wiki/Duck_typing]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has white and gray feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The person takes a feather from the ground and shows it.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  john = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest john&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Output of the above program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has white and gray feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
The person takes a feather from the ground and shows it.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
*[http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them (1)http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them]&lt;br /&gt;
*[http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby (2)http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Closure_(computer_science) (3)http://en.wikipedia.org/wiki/Closure_(computer_science)]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Continuation-passing_style (4)http://en.wikipedia.org/wiki/Continuation-passing_style]&lt;br /&gt;
*[http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177/ref=sr_1_1?ie=UTF8&amp;amp;qid=1318716890&amp;amp;sr=8-1 (5)Flanagan, D and Matsumoto, Y, (2008). The Ruby Programming Language, O’Reilly Media, Inc.,]&lt;br /&gt;
*[http://www.amazon.com/Learning-Ruby-Michael-James-Fitzgerald/dp/0596529864/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1318716919&amp;amp;sr=1-1 (6)Fitzgerald Michael, (2007). Learning Ruby, O’Reilly Media, Inc., (example)]&lt;br /&gt;
*[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ (7)http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/]&lt;br /&gt;
*[http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/ (8)http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/]&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52602</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52602"/>
		<updated>2011-10-19T14:12:17Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Currying */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ (7)]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them (1)].  For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies [http://en.wikipedia.org/wiki/Duck_typing]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has white and gray feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The person takes a feather from the ground and shows it.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  john = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest john&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Output of the above program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has white and gray feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
The person takes a feather from the ground and shows it.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
*[http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them (1)http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them]&lt;br /&gt;
*[http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby (2)http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Closure_(computer_science) (3)http://en.wikipedia.org/wiki/Closure_(computer_science)]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Continuation-passing_style (4)http://en.wikipedia.org/wiki/Continuation-passing_style]&lt;br /&gt;
*[http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177/ref=sr_1_1?ie=UTF8&amp;amp;qid=1318716890&amp;amp;sr=8-1 (5)Flanagan, D and Matsumoto, Y, (2008). The Ruby Programming Language, O’Reilly Media, Inc.,]&lt;br /&gt;
*[http://www.amazon.com/Learning-Ruby-Michael-James-Fitzgerald/dp/0596529864/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1318716919&amp;amp;sr=1-1 (6)Fitzgerald Michael, (2007). Learning Ruby, O’Reilly Media, Inc., (example)]&lt;br /&gt;
*[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ (7)http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/]&lt;br /&gt;
*[http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/ (8)http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/]&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52601</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52601"/>
		<updated>2011-10-19T14:05:07Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Attributes, Instance Variables, and Methods */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ (7)]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them (1)].  For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute from the method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies [http://en.wikipedia.org/wiki/Duck_typing]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has white and gray feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The person takes a feather from the ground and shows it.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  john = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest john&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Output of the above program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has white and gray feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
The person takes a feather from the ground and shows it.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
*[http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them (1)http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them]&lt;br /&gt;
*[http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby (2)http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Closure_(computer_science) (3)http://en.wikipedia.org/wiki/Closure_(computer_science)]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Continuation-passing_style (4)http://en.wikipedia.org/wiki/Continuation-passing_style]&lt;br /&gt;
*[http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177/ref=sr_1_1?ie=UTF8&amp;amp;qid=1318716890&amp;amp;sr=8-1 (5)Flanagan, D and Matsumoto, Y, (2008). The Ruby Programming Language, O’Reilly Media, Inc.,]&lt;br /&gt;
*[http://www.amazon.com/Learning-Ruby-Michael-James-Fitzgerald/dp/0596529864/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1318716919&amp;amp;sr=1-1 (6)Fitzgerald Michael, (2007). Learning Ruby, O’Reilly Media, Inc., (example)]&lt;br /&gt;
*[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ (7)http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/]&lt;br /&gt;
*[http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/ (8)http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/]&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52600</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52600"/>
		<updated>2011-10-19T05:32:15Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Attributes, Instance Variables, and Methods */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ (7)]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them (1)].  For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object [http://books.google.com/books/about/Programming_Ruby.html?id=-YVQAAAAMAAJ]&lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute with method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies [http://en.wikipedia.org/wiki/Duck_typing]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has white and gray feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The person takes a feather from the ground and shows it.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  john = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest john&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Output of the above program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has white and gray feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
The person takes a feather from the ground and shows it.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
*[http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them (1)http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them]&lt;br /&gt;
*[http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby (2)http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Closure_(computer_science) (3)http://en.wikipedia.org/wiki/Closure_(computer_science)]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Continuation-passing_style (4)http://en.wikipedia.org/wiki/Continuation-passing_style]&lt;br /&gt;
*[http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177/ref=sr_1_1?ie=UTF8&amp;amp;qid=1318716890&amp;amp;sr=8-1 (5)Flanagan, D and Matsumoto, Y, (2008). The Ruby Programming Language, O’Reilly Media, Inc.,]&lt;br /&gt;
*[http://www.amazon.com/Learning-Ruby-Michael-James-Fitzgerald/dp/0596529864/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1318716919&amp;amp;sr=1-1 (6)Fitzgerald Michael, (2007). Learning Ruby, O’Reilly Media, Inc., (example)]&lt;br /&gt;
*[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ (7)http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/]&lt;br /&gt;
*[http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/ (8)http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/]&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52599</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52599"/>
		<updated>2011-10-19T05:30:03Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Attributes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ (7)]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them (1)].  For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html]&lt;br /&gt;
&lt;br /&gt;
Ruby provides convenient methods to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut i.e. if you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object. &lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute with method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies [http://en.wikipedia.org/wiki/Duck_typing]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has white and gray feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The person takes a feather from the ground and shows it.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  john = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest john&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Output of the above program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has white and gray feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
The person takes a feather from the ground and shows it.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
*[http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them (1)http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them]&lt;br /&gt;
*[http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby (2)http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Closure_(computer_science) (3)http://en.wikipedia.org/wiki/Closure_(computer_science)]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Continuation-passing_style (4)http://en.wikipedia.org/wiki/Continuation-passing_style]&lt;br /&gt;
*[http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177/ref=sr_1_1?ie=UTF8&amp;amp;qid=1318716890&amp;amp;sr=8-1 (5)Flanagan, D and Matsumoto, Y, (2008). The Ruby Programming Language, O’Reilly Media, Inc.,]&lt;br /&gt;
*[http://www.amazon.com/Learning-Ruby-Michael-James-Fitzgerald/dp/0596529864/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1318716919&amp;amp;sr=1-1 (6)Fitzgerald Michael, (2007). Learning Ruby, O’Reilly Media, Inc., (example)]&lt;br /&gt;
*[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ (7)http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/]&lt;br /&gt;
*[http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/ (8)http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/]&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52598</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52598"/>
		<updated>2011-10-19T05:21:49Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Duck Typing (Unbounded Polymorphism) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ (7)]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them (1)].  For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes.&lt;br /&gt;
&lt;br /&gt;
Ruby provides some convenient method to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut. If you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object. &lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute with method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies [http://en.wikipedia.org/wiki/Duck_typing]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has white and gray feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The person takes a feather from the ground and shows it.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  john = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest john&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
game&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Output of the above program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has white and gray feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
The person takes a feather from the ground and shows it.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
*[http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them (1)http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them]&lt;br /&gt;
*[http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby (2)http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Closure_(computer_science) (3)http://en.wikipedia.org/wiki/Closure_(computer_science)]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Continuation-passing_style (4)http://en.wikipedia.org/wiki/Continuation-passing_style]&lt;br /&gt;
*[http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177/ref=sr_1_1?ie=UTF8&amp;amp;qid=1318716890&amp;amp;sr=8-1 (5)Flanagan, D and Matsumoto, Y, (2008). The Ruby Programming Language, O’Reilly Media, Inc.,]&lt;br /&gt;
*[http://www.amazon.com/Learning-Ruby-Michael-James-Fitzgerald/dp/0596529864/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1318716919&amp;amp;sr=1-1 (6)Fitzgerald Michael, (2007). Learning Ruby, O’Reilly Media, Inc., (example)]&lt;br /&gt;
*[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ (7)http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/]&lt;br /&gt;
*[http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/ (8)http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/]&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52596</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52596"/>
		<updated>2011-10-19T05:20:45Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Duck Typing (Unbounded Polymorphism) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ (7)]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them (1)].  For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes.&lt;br /&gt;
&lt;br /&gt;
Ruby provides some convenient method to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut. If you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object. &lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute with method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has white and gray feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The person takes a feather from the ground and shows it.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  john = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest john&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Output of the above program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has white and gray feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
The person takes a feather from the ground and shows it.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
*[http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them (1)http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them]&lt;br /&gt;
*[http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby (2)http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Closure_(computer_science) (3)http://en.wikipedia.org/wiki/Closure_(computer_science)]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Continuation-passing_style (4)http://en.wikipedia.org/wiki/Continuation-passing_style]&lt;br /&gt;
*[http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177/ref=sr_1_1?ie=UTF8&amp;amp;qid=1318716890&amp;amp;sr=8-1 (5)Flanagan, D and Matsumoto, Y, (2008). The Ruby Programming Language, O’Reilly Media, Inc.,]&lt;br /&gt;
*[http://www.amazon.com/Learning-Ruby-Michael-James-Fitzgerald/dp/0596529864/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1318716919&amp;amp;sr=1-1 (6)Fitzgerald Michael, (2007). Learning Ruby, O’Reilly Media, Inc., (example)]&lt;br /&gt;
*[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ (7)http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/]&lt;br /&gt;
*[http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/ (8)http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/]&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52595</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52595"/>
		<updated>2011-10-19T05:20:11Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Duck Typing (Unbounded Polymorphism) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ (7)]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them (1)].  For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes.&lt;br /&gt;
&lt;br /&gt;
Ruby provides some convenient method to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut. If you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object. &lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute with method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has white and gray feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The person takes a feather from the ground and shows it.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  john = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest john&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Output of the above program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has white and gray feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
The person takes a feather from the ground and shows it.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
*[http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them (1)http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them]&lt;br /&gt;
*[http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby (2)http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Closure_(computer_science) (3)http://en.wikipedia.org/wiki/Closure_(computer_science)]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Continuation-passing_style (4)http://en.wikipedia.org/wiki/Continuation-passing_style]&lt;br /&gt;
*[http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177/ref=sr_1_1?ie=UTF8&amp;amp;qid=1318716890&amp;amp;sr=8-1 (5)Flanagan, D and Matsumoto, Y, (2008). The Ruby Programming Language, O’Reilly Media, Inc.,]&lt;br /&gt;
*[http://www.amazon.com/Learning-Ruby-Michael-James-Fitzgerald/dp/0596529864/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1318716919&amp;amp;sr=1-1 (6)Fitzgerald Michael, (2007). Learning Ruby, O’Reilly Media, Inc., (example)]&lt;br /&gt;
*[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ (7)http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/]&lt;br /&gt;
*[http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/ (8)http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/]&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52593</id>
		<title>CSC/ECE 517 Fall 2011/ch3 4b js</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_4b_js&amp;diff=52593"/>
		<updated>2011-10-19T05:19:38Z</updated>

		<summary type="html">&lt;p&gt;Vsingh3: /* Duck Typing (Unbounded Polymorphism) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Closures ==&lt;br /&gt;
===What is a Closure?===&lt;br /&gt;
A closure is a block of code that can access the lexical environment of its definition.  A closure has two properties: &lt;br /&gt;
*A closure can be passed as an object.&lt;br /&gt;
*A closure recalls the values of all the variables that were in scope when the function was created and is able to access those variables when it is called even though they may no longer be in scope.&lt;br /&gt;
A closure can be expressed succintly as a function pointer that references a block of executable code and the variables from the scope it was created.&lt;br /&gt;
&lt;br /&gt;
===Closures in Ruby=== &lt;br /&gt;
Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are blocks. &lt;br /&gt;
====Blocks==== &lt;br /&gt;
In Ruby, blocks are used to group statements usually enclosed by braces or between a &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; and occur solely in the source adjacent to a method call.  Rule of thumb in Ruby is to use braces for blocks containing only a single line and &amp;lt;b&amp;gt;do...end&amp;lt;/b&amp;gt; for multi-line blocks.  The code within the block is not executed when encountered, instead, Ruby recalls the context of the block and then enters the method.  Typically, the blocks passed into methods are anonymous objects that are created instantly, but they can be instantiated as a Proc object by using either the &amp;lt;b&amp;gt; proc &amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method.  Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ (7)]  Proc objects can be simply thought of as anonymous function objects.  Proc objects can be stored, created at runtime, passed as arguments, retrieved, as well as, be returned as values.  Consider the example below from [http://www.csc.ncsu.edu/faculty/efg/517/f11/lectures#j10 lecture 5]:&lt;br /&gt;
&lt;br /&gt;
Class to generate adders:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class AdderGen&lt;br /&gt;
   def initialize(n)&lt;br /&gt;
      @block = lambda {|a| n + a }&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def add(a)&lt;br /&gt;
      @block.call a&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
twoAdder    = AdderGen.new 2&lt;br /&gt;
incrementer = AdderGen.new 1&lt;br /&gt;
&lt;br /&gt;
puts incrementer.add(4)&lt;br /&gt;
puts twoAdder.add(6)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; 5&lt;br /&gt;
&amp;gt;&amp;gt; 8&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above, the instance variable &amp;lt;b&amp;gt; @block &amp;lt;/b&amp;gt; is a closure and it recalls the parameter from which the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method was called after the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method exits and it is used when the block is called in the &amp;lt;b&amp;gt; add &amp;lt;/b&amp;gt; method.  This example also uses the &amp;lt;b&amp;gt; lambda &amp;lt;/b&amp;gt; method to generate new functions dynamically, which will be discussed in the next section.&lt;br /&gt;
&lt;br /&gt;
====Procs and Lambdas====&lt;br /&gt;
When creating a &amp;lt;b&amp;gt;lambda&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;proc&amp;lt;/b&amp;gt;, the object holds a reference to the executable block and bindings for all the variables used by the block.  In Ruby, we can create a Proc object explicitly in three different ways:&lt;br /&gt;
&lt;br /&gt;
====Proc.new====&lt;br /&gt;
Using this method involves simply passing in a block, which will return a Proc object that will run the code in the block when you invoke its call method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = Proc.new {puts “This is a proc object.”}&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====proc method====  &lt;br /&gt;
In the Kernel module, we can use the proc method, which is available globally.  In Ruby 1.9, it is equivalent to Proc.new, but in Ruby 1.8, it is equivalent to lambda.  The difference between proc and lambda will be discussed later. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = proc { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====lambda method====&lt;br /&gt;
Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pobject = lambda { puts “Inside the proc object” }&lt;br /&gt;
pobject.call &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== procs vs. lambdas ===&lt;br /&gt;
One difference between the proc kernel method and the lambda kernel method is that the lambda kernel method re&lt;br /&gt;
\\esults in an object that checks the number of its arguments, while a plain Proc.new does not:&lt;br /&gt;
Another difference between &amp;lt;b&amp;gt;procs&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;lambdas&amp;lt;/b&amp;gt; is their handling of control flow keywords, such as &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt;.  Consider the examples below [http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them (1)].  For instance, the first example below shows the difference in behavior using the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; keyword.  With the proc method, the &amp;lt;b&amp;gt;return&amp;lt;/b&amp;gt; not only returns from the proc method, but also the enclosing method, which is shown when the last &amp;lt;b&amp;gt;puts&amp;lt;/b&amp;gt; is not called.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If we exchange the proc for the lambda method, the &amp;lt;b&amp;gt;return &amp;lt;/b&amp;gt; only returns from the lambda method and NOT the enclosing method, thus, it continues on to execute the last &amp;lt;b&amp;gt; puts &amp;lt;/b&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    return&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we go back to the proc method, and use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword in lieu of &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt;.  However, instead of returning from the proc method and the enclosing method, we are issued a &amp;lt;i&amp;gt; LocalJumpError &amp;lt;/i&amp;gt;.  Since &amp;lt;b&amp;gt; break&amp;lt;/b&amp;gt; keywords are usually used to fall out of  an iteration, but it is not contained within an iteration, Ruby throws an error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = Proc.new do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)&lt;br /&gt;
    from a.rb:66:in `call'&lt;br /&gt;
    from a.rb:66:in `my_method'&lt;br /&gt;
    from a.rb:70:in `&amp;lt;main&amp;gt;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us revisit the lambda method, but this time, we will use the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; keyword.  You will notice that the &amp;lt;b&amp;gt; break &amp;lt;/b&amp;gt; is treated like the &amp;lt;b&amp;gt; return &amp;lt;/b&amp;gt; keyword, where the execution is dumped out of the lambda, but continues to execute the rest of the enclosing method. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def my_method&lt;br /&gt;
  puts &amp;quot;before proc&amp;quot;&lt;br /&gt;
  my_proc = lambda do&lt;br /&gt;
    puts &amp;quot;inside proc&amp;quot;&lt;br /&gt;
    break&lt;br /&gt;
  end&lt;br /&gt;
  my_proc.call&lt;br /&gt;
  puts &amp;quot;after proc&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
my_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
user@user-ubuntu:/home/user/ruby$ ruby a.rb&lt;br /&gt;
before proc&lt;br /&gt;
inside proc&lt;br /&gt;
after proc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why use Closures?===&lt;br /&gt;
&lt;br /&gt;
Closures not only enable programmers to write logic with less code, they are also useful in iterating over lists and encapsulation operations that require setup and clean-up afterwards.&lt;br /&gt;
&lt;br /&gt;
==Currying ==&lt;br /&gt;
&lt;br /&gt;
Currying is a concept in Functional Programming that’s enabled by Higher-order functions. It is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application) [http://www.khelll.com/blog/ruby/ruby-currying/]&lt;br /&gt;
Given a function f of type &amp;lt;pre&amp;gt;f:(X x Y)--&amp;gt;Z&amp;lt;/pre&amp;gt; &lt;br /&gt;
Currying it makes a function &amp;lt;pre&amp;gt;curry(f): X--&amp;gt;(Y--&amp;gt;Z)&amp;lt;/pre&amp;gt;&lt;br /&gt;
That is curry(f) takes an argument of type X and&lt;br /&gt;
Returns a function of type &amp;lt;pre&amp;gt;Y--&amp;gt;Z&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following method is to sum all integers, to sum the squares of all integers and to sum the powers 2^n of all integers n between two given numbers a and b.&lt;br /&gt;
&lt;br /&gt;
// Normal definitions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sum_ints = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n } ; s &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
sum_of_squares = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += n**2 } ;s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
sum_of_powers_of_2 = lambda do |a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += 2**n } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// After factoring out the common pattern by defining a method sum&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Some refactoring to make some abstraction&lt;br /&gt;
# Passing the sum mechanism itself&lt;br /&gt;
&lt;br /&gt;
sum = lambda do |f,a,b|&lt;br /&gt;
  s = 0 ; a.upto(b){|n| s += f.(n) } ; s &lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
puts sum.(lambda{|x| x},1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum.(lambda{|x| x**2},1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum.(lambda{|x| 2**x},1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Passing the first param to the sum method to specify what type of sum it is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Refactoring using currying &lt;br /&gt;
# generate the currying&lt;br /&gt;
&lt;br /&gt;
currying = sum.curry&lt;br /&gt;
 &lt;br /&gt;
 Generate the partial functions&lt;br /&gt;
sum_ints = currying.(lambda{|x| x})&lt;br /&gt;
sum_of_squares = currying.(lambda{|x| x**2})&lt;br /&gt;
sum_of_powers_of_2 = currying.(lambda{|x| 2**x})&lt;br /&gt;
 &lt;br /&gt;
puts sum_ints.(1,5) #=&amp;gt; 15&lt;br /&gt;
puts sum_of_squares.(1,5) #=&amp;gt; 55&lt;br /&gt;
puts sum_of_powers_of_2.(1,5) #=&amp;gt; 62&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Object-Oriented Programming in Ruby ==&lt;br /&gt;
Ruby is the ideal Object Oriented Programming Language, which has four essential properties:&lt;br /&gt;
*Data Encapsulation&lt;br /&gt;
*Data Abstraction&lt;br /&gt;
*Polymorphism&lt;br /&gt;
*Inheritance&lt;br /&gt;
&lt;br /&gt;
An object-oriented program consists of classes and objects.  An object consists of state and related behavior, where its state is contained within fields and allows access to its behavior through use of methods. These methods can alter an object's state and serve as the facilitator for communication between objects.  Classes are defined as the building blocks from which objects are created and will be explained further as we progress through the four properties through the subsequent sections.&lt;br /&gt;
&lt;br /&gt;
===Classes ===&lt;br /&gt;
&lt;br /&gt;
To learn more about classes, let us first define a basic  &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;b&amp;gt; class &amp;lt;/b&amp;gt; is used to define a class type with the class name in camel case, and the keyword &amp;lt;b&amp;gt; end &amp;lt;/b&amp;gt; denotes the end of the code block.  The &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above explicitly inherits from Object.  As we stated before, a class usually has state and behavior, so we will add instance variables, &amp;lt;b&amp;gt; fname&amp;lt;/b&amp;gt; and &amp;lt;b&amp;gt; lname &amp;lt;/b&amp;gt;, as well as, the necessary accessor methods, getters and setters and a class variable &amp;lt;b&amp;gt; count &amp;lt;/b&amp;gt; to keep track of the number of instances of the class created.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
&lt;br /&gt;
   @@count = 0&lt;br /&gt;
&lt;br /&gt;
   attr_accessor :fname, :lname.&lt;br /&gt;
&lt;br /&gt;
   def initialize(fname,lname)&lt;br /&gt;
     @fname = fname&lt;br /&gt;
     @lname = lname&lt;br /&gt;
     @@count += 1&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def self.info&lt;br /&gt;
      puts &amp;quot;Number of instances = #{@@count}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def to_s&lt;br /&gt;
     &amp;quot;Name: #{@fname} #{@lname}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt; attr_accessor &amp;lt;/b&amp;gt; method dynamically defines the read and write methods for the two instance variables.  To instantiate an object of the new class, we use &amp;lt;b&amp;gt; &amp;lt;varname&amp;gt; = &amp;lt;classname&amp;gt;.new(arg1,arg2)&amp;lt;/b&amp;gt;, which automatically calls the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method of the class and passes the arguments to the &amp;lt;b&amp;gt; initialize &amp;lt;/b&amp;gt; method.  Using our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class defined above, we can instantiate objects as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
pers1 = Person.new(&amp;quot;John&amp;quot;,&amp;quot;Smith&amp;quot;)&lt;br /&gt;
pers2 = Person.new(&amp;quot;Tammy&amp;quot;,&amp;quot;Lee&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Instance Methods====&lt;br /&gt;
Ruby methods are defined using the keyword &amp;lt;b&amp;gt; def &amp;lt;/b&amp;gt;, where the signature is the method name.    The &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method returns a String representation of the object, so for the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt;, it returns a string concatenation of the first and last name.  Since the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class is already defined in Object, and we have redefined it in our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class, Ruby will use the last implementation of the method.  Since Ruby returns the value of the last expression of a method as the return value, we can in fact invoke the &amp;lt;b&amp;gt; to_s &amp;lt;/b&amp;gt; method in two ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Implicit call to_s&lt;br /&gt;
puts pers1&lt;br /&gt;
&lt;br /&gt;
#Explicit call to_s&lt;br /&gt;
str_name = pers1.to_s&lt;br /&gt;
puts str_name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Class Methods====&lt;br /&gt;
In Ruby, class methods actually belong to the class and not the instantiation of an object, such as above.  In our &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class above, the method &amp;lt;b&amp;gt; self.info &amp;lt;/b&amp;gt; is actually a class method, which prints the number of instances of the &amp;lt;b&amp;gt; Person &amp;lt;/b&amp;gt; class.  A class method is defined by prefixing the method name with the keyword &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt;.  When inside an instance method, &amp;lt;b&amp;gt; self &amp;lt;/b&amp;gt; is the receiver object, whereas, outside of an instance method, but within a class definition, self, in fact, refers to the class.  To invoke a class method, you must call the class method by prefixing it with the class name, where it will resolve to the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Print number of instances.&lt;br /&gt;
puts Person.info&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Attributes===&lt;br /&gt;
&lt;br /&gt;
Usually methods are defined to give access and power to change the state of an object. If the objects created have a private state, it means no other object can access its instance variables. In other words, object itself is responsible for maintaining its own consistency, however it’s not of much use since you can’t do anything with it. One wants to create methods so that outside world can interact with it. These external visible aspects of an object are called its attributes.&lt;br /&gt;
&lt;br /&gt;
Ruby provides some convenient method to define accessor methods: attr_accessor, attr_writer and attr_reader. Attributes are just a shortcut. If you use attr_accessor to create an attribute, Ruby just declares an instance variable and creates getter and setter methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Thing&lt;br /&gt;
    attr_accessor :my_property&lt;br /&gt;
    attr_reader :my_readable_property&lt;br /&gt;
    attr_writer :my_writable_property&lt;br /&gt;
&lt;br /&gt;
    def do_stuff&lt;br /&gt;
        # does stuff&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writable Attributes====&lt;br /&gt;
&lt;br /&gt;
Sometimes you need to be able to set an attribute from outside the object. &lt;br /&gt;
In Ruby, the attributes of an object can be accessed as if they were any other variable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def duration=(newDuration)&lt;br /&gt;
    @duration = newDuration&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration	»	260&lt;br /&gt;
aSong.duration = 257   # set attribute with updated value&lt;br /&gt;
aSong.duration	»	257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby also provides a shortcut for creating these simple attribute-setting methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  attr_writer :duration&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.duration = 257&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Virtual Attributes====&lt;br /&gt;
&lt;br /&gt;
Attribute-accessing methods don’t just have to be simple wrappers around an object's instance variables. In the following example, attribute methods have been used to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other but internally; there is no corresponding instance variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Song&lt;br /&gt;
  def durationInMinutes&lt;br /&gt;
    @duration/60.0   # force floating point&lt;br /&gt;
  end&lt;br /&gt;
  def durationInMinutes=(value)&lt;br /&gt;
    @duration = (value*60).to_i&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
aSong = Song.new(&amp;quot;Bicylops&amp;quot;, &amp;quot;Fleck&amp;quot;, 260)&lt;br /&gt;
aSong.durationInMinutes	»	4.333333333&lt;br /&gt;
aSong.durationInMinutes = 4.2&lt;br /&gt;
aSong.duration	»	252&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bertrand Meyer calls this as Uniform Access Principle in book Object-Oriented Software Construction. This way the implementation of the class is hidden from the world and is a big advantage.&lt;br /&gt;
====Attributes, Instance Variables, and Methods====&lt;br /&gt;
&lt;br /&gt;
An attribute can act as a method. It can return the value of an instance variable or result of a calculation and sometimes these methods with equals signs at the end of their names are used to update the state of an object. &lt;br /&gt;
&lt;br /&gt;
To differentiate an attribute with method, Dave Thomas, author of the book, Programming ruby, The pragmatic programmers guide, quotes “When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written.”&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
===Access Control===&lt;br /&gt;
&lt;br /&gt;
Allowing much access into class has a risk of increased coupling in the application. Users in this case tend to depend on the class's implementation, rather than on its logical interface. Since the only way to change the state of an object is through calling its methods. Hence, Ruby gives us three levels of protection.&lt;br /&gt;
&lt;br /&gt;
•      Public methods can be called by anyone and there is no access control. Methods are public by default (except for initialize, which is always private).&lt;br /&gt;
&lt;br /&gt;
•	Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
&lt;br /&gt;
•	Private methods cannot be called with an explicit receiver. Since you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.&lt;br /&gt;
&lt;br /&gt;
If a method is protected, it may be called by any instance of the class or its subclasses. If a method is private, it may be called only within the context of the calling object. One can never access another objects private methods directly, even if the object is of the same class as the caller.&lt;br /&gt;
&lt;br /&gt;
Ruby differs from other OO languages in terms of access control i.e. it is determined dynamically, as the program runs, not statically. One will get an access violation only when the code attempts to execute the restricted method.&lt;br /&gt;
&lt;br /&gt;
====Specifying Access Control====&lt;br /&gt;
Access levels are specified to methods within class or module definitions using one or more of these three functions public, protected, and private. Each function can be used in two different ways.&lt;br /&gt;
&lt;br /&gt;
If no arguments are passed, the three functions set the default access control of subsequently defined methods [http://rubylearning.com/satishtalim/ruby_access_control.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1          # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  protected  &lt;br /&gt;
    def m2        # this method is protected  &lt;br /&gt;
    end  &lt;br /&gt;
  private  &lt;br /&gt;
    def m3        # this method is private  &lt;br /&gt;
    end  &lt;br /&gt;
end  &lt;br /&gt;
ca = ClassAccess.new  &lt;br /&gt;
ca.m1  &lt;br /&gt;
#ca.m2  &lt;br /&gt;
#ca.m3  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removing the comments from the last two statements in the above program, will give an access violation runtime error.&lt;br /&gt;
Alternatively, one can set access levels of named methods by listing them as arguments to the access control functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ClassAccess  &lt;br /&gt;
  def m1       # this method is public  &lt;br /&gt;
  end  &lt;br /&gt;
  public :m1  &lt;br /&gt;
  protected :m2, :m3  &lt;br /&gt;
  private :m4, :m5  &lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class's initialize method is automatically declared to be private.&lt;br /&gt;
&lt;br /&gt;
Protected access is used when objects need to access the internal state of other objects of the same class.&lt;br /&gt;
&lt;br /&gt;
====Public, Protected and Private Visibility====&lt;br /&gt;
&lt;br /&gt;
Summing it up [http://adhirajrankhambe.wordpress.com/2010/05/02/access-control-in-ruby/]&lt;br /&gt;
&lt;br /&gt;
=====Public Visibility=====&lt;br /&gt;
#	The default access level is “Public”. Public methods are accessible from anywhere (within the class, outside the class, same instance, other instances of same class and sub-classes).&lt;br /&gt;
&lt;br /&gt;
=====Protected Visibility=====&lt;br /&gt;
#	Protected methods are visible to all the instances of the same class as well as sub-classes.&lt;br /&gt;
#	Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.&lt;br /&gt;
#	While accessing protected methods, you can specify the receiver; receiver can by explicit or implicit.&lt;br /&gt;
#	“Protected” in Ruby is not quite common in other programming languages.&lt;br /&gt;
#	Private and Protected methods are not directly accessible outside the class.&lt;br /&gt;
&lt;br /&gt;
=====Private Visibility=====&lt;br /&gt;
#	Private methods are private to the instance (and not to the class). &lt;br /&gt;
#	Private methods are accessible from sub-classes.  &lt;br /&gt;
#	While accessing private methods, one cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.&lt;br /&gt;
#	“Private” in Ruby is what “Protected” is in many other languages.&lt;br /&gt;
#	 In Ruby, no method is perfectly private or hidden.&lt;br /&gt;
&lt;br /&gt;
===Abstract Methods===&lt;br /&gt;
&lt;br /&gt;
===Duck Typing (Unbounded Polymorphism)===&lt;br /&gt;
&lt;br /&gt;
Relying less on the type (or class) of an object and more on its capabilities is called duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; duck &amp;lt;&amp;lt; &amp;quot;quack&amp;quot; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, duck can be a String, a File, or an Array (which is very useful when writing unit tests) [ http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78502]&lt;br /&gt;
&lt;br /&gt;
Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. There is no need for testing the type of arguments in method and function bodies.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Duck&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;Quaaaaaack!&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The duck has white and gray feathers.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Person&lt;br /&gt;
  def quack&lt;br /&gt;
    puts &amp;quot;The person imitates a duck.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def feathers&lt;br /&gt;
    puts &amp;quot;The person takes a feather from the ground and shows it.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def in_the_forest duck&lt;br /&gt;
  duck.quack&lt;br /&gt;
  duck.feathers&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def game&lt;br /&gt;
  donald = Duck.new&lt;br /&gt;
  john = Person.new&lt;br /&gt;
  in_the_forest donald&lt;br /&gt;
  in_the_forest john&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Output of the above program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Quaaaaaack!&lt;br /&gt;
The duck has white and gray feathers.&lt;br /&gt;
The person imitates a duck.&lt;br /&gt;
The person takes a feather from the ground and shows it.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a non-duck-typed language, one can create a function that takes an object of type Duck/Person and calls that object's walk and feathers methods. In a duck-typed language as shown above, the equivalent function would take an object of any type and call that object's walk and feathers methods. If the object does not have the methods that are called then the function signals a run-time error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
*[http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them (1)http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them]&lt;br /&gt;
*[http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby (2)http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Closure_(computer_science) (3)http://en.wikipedia.org/wiki/Closure_(computer_science)]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Continuation-passing_style (4)http://en.wikipedia.org/wiki/Continuation-passing_style]&lt;br /&gt;
*[http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177/ref=sr_1_1?ie=UTF8&amp;amp;qid=1318716890&amp;amp;sr=8-1 (5)Flanagan, D and Matsumoto, Y, (2008). The Ruby Programming Language, O’Reilly Media, Inc.,]&lt;br /&gt;
*[http://www.amazon.com/Learning-Ruby-Michael-James-Fitzgerald/dp/0596529864/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1318716919&amp;amp;sr=1-1 (6)Fitzgerald Michael, (2007). Learning Ruby, O’Reilly Media, Inc., (example)]&lt;br /&gt;
*[http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/ (7)http://blog.mostof.it/why-ruby-part-two-blocks-and-closures/]&lt;br /&gt;
*[http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/ (8)http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/]&lt;/div&gt;</summary>
		<author><name>Vsingh3</name></author>
	</entry>
</feed>