CSC/ECE 517 Fall 2011/ch3 4b js: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 57: Line 57:
     puts "inside proc"
     puts "inside proc"
     return
     return
  end
  my_proc.call
  puts "after proc"
end
my_method
</pre>
<pre>
user@user-ubuntu:/home/user/ruby$ ruby a.rb
before proc
inside proc
after proc
</pre>
-------------------
<pre>
def my_method
  puts "before proc"
  my_proc = Proc.new do
    puts "inside proc"
    break
  end
  my_proc.call
  puts "after proc"
end
my_method
</pre>
<pre>
user@user-ubuntu:/home/user/ruby$ ruby a.rb
before proc
inside proc
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)
    from a.rb:66:in `call'
    from a.rb:66:in `my_method'
    from a.rb:70:in `<main>'
</pre>
<pre>
def my_method
  puts "before proc"
  my_proc = lambda do
    puts "inside proc"
    break
   end
   end
   my_proc.call
   my_proc.call

Revision as of 04:00, 15 October 2011

Closures

What is a Closure?

A closure is a block of code that can access the lexical environment of its definition. A closure has two properties:

  • A closure can be passed as an object.
  • 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.

A closure can be expressed simply as a function pointer that references a block of executable code and the variables from the scope it was created.

Closures in Ruby

Ruby supports closures through use of Procedures, or simply procs, and lambdas, which are Blocks. When creating a lambda or proc, the object holds a reference to the executable block and bindings for all the variables used by the block.

Blocks

Procs and Lambdas

In Ruby, we can create a Proc object explicitly in three different ways.

Proc.new

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.

pobject = Proc.new {puts “This is a proc object.”}
pobject.call 

proc method

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.

pobject = proc { puts “Inside the proc object” }
pobject.call 

lambda method

Similar to the proc method, it is globally available in the Kernel module, however, it will create a lambda Proc object.

pobject = lambda { puts “Inside the proc object” }
pobject.call 

procs vs. lambdas

The difference between Procs and Lambdas has to do with their handling of control flow keywords, such as break and return.

def my_method
  puts "before proc"
  my_proc = Proc.new do
    puts "inside proc"
    return
  end
  my_proc.call
  puts "after proc"
end
 
my_method
user@user-ubuntu:/home/user/ruby$ ruby a.rb
before proc
inside proc
def my_method
  puts "before proc"
  my_proc = lambda do
    puts "inside proc"
    return
  end
  my_proc.call
  puts "after proc"
end
 
my_method
user@user-ubuntu:/home/user/ruby$ ruby a.rb
before proc
inside proc
after proc

def my_method
  puts "before proc"
  my_proc = Proc.new do
    puts "inside proc"
    break
  end
  my_proc.call
  puts "after proc"
end
 
my_method
user@user-ubuntu:/home/user/ruby$ ruby a.rb
before proc
inside proc
a.rb:64:in `block in my_method': break from proc-closure (LocalJumpError)
    from a.rb:66:in `call'
    from a.rb:66:in `my_method'
    from a.rb:70:in `<main>'
def my_method
  puts "before proc"
  my_proc = lambda do
    puts "inside proc"
    break
  end
  my_proc.call
  puts "after proc"
end
 
my_method


user@user-ubuntu:/home/user/ruby$ ruby a.rb
before proc
inside proc
after proc

Why use Closures?

Currying

Object-Oriented Programming in Ruby

Classes

Attributes

Inheritance

Access Control

Abstract Methods