Proc Classes in Ruby and Proc class methods Proc.new usage Resolution _ruby topics

Source: Internet
Author: User
Tags closure variable scope

Proc is a process object which is obtained after the object processing of block and its context (local variable scope and stack frame). You can use proc like a nameless function, but it does not import the scope of local variables (you can use dynamic local variables as proc local variables).

In the following example, the Var variable is called because the proc always holds the scope of the local variable.

var = 1
$foo = proc.new {var}
var = 2

def foo
 $foo. Call
end

p foo    # => 2

After returning from a method that generates proc, a Localjumperror exception is thrown if return or retry occurs in Proc.

def foo
 proc {return}
"

foo.call
# => in ' call": Return from Proc-closure (localjumperror) 
   def foo
 proc {retry}
end

foo.call
# => in ' call ': Retry from Proc-closure (localjumperror)

If you precede the proc with "&" and pass it to a method with a block, the operation is similar to the call block. But in the strict sense, there are the following differences.

# no problem
(1..5). Each {break}

# is OK in Ruby 1.6.7, 1.8.
an exception proc = proc.new {break}
(1..5) occurs in 1.6.8. each (&proc)

# is localjumperror in Ruby 1.6
# in Ruby 1.8, Run each
proc = proc.new {retry}
(1..5) again. each (&proc)
#=> retry from Proc-closure (localjumperror)

This is exactly the limit that the Proc object uses as a call block.

Proc.new
proc.new {...}

The result is returned after the object of the block and its context is processed.

If no block is given, the block to which the method is invoked is converted to a Proc object and returned.

def foo
  PR = proc.new
  pr.call (1,2,3)
end
foo {|args| P args}
# => [1, 2, 3]

Proc.new method in Depth
proc.new The block and its context, and returns the result after the object is processed.

If no block is given, the block to which the method is invoked is converted to a Proc object and returned.

def foo
  PR = proc.new
  pr.call (1,2,3)
end
foo {|args| P args}
# => [1, 2, 3]
this is the same as the following example
def foo
 yield (1,2,3)
end
foo {|args| P args}
# => [1, 2, 3]

If the keynote method does not have a block, the Argumenterror exception is thrown.

def foo
 proc.new
end
foo
# =>-:2:in ' new ': Tried to-create Proc object without a block (argumenter ROR) from
     -:2:in ' foo ' to
     -:4

When Proc.new is used, the method is called when the object is initialized, if the Proc#initialize method is defined. Besides, it is the same as Proc.

You can create a Proc object that represents a block by using the Proc.new method, or by specifying a block for the Proc method.

Executes the block by calling the Proc#call method. When the Proc#call method is invoked, the argument is used as a block variable, and the value of the last expression in the block is the return value of Proc#call. Proc#call also has a name called proc#[].

# to judge whether the year of Western calendar is the processing leap for leap years
= proc.new do |year|
 Year% 4 = 0 && Year% 100!= 0 | | Year% ==0 end
P Leap.call (Watts)  #=> true
p leap[2013]     #=> false
p leap[2016]< C10/>#=> true 

Set block variable to |* array | form, you can receive a variable number of arguments as an array, as with method parameters.

Double = proc.new do |*args|
 args.map{|i| i * 2}  # all elements multiply by twice times end
P double.call (1, 2, 3)  #=> [2, 3, 4]
p double[2, 3, 4 ]     #=> [4, 6, 8] 

In addition, the types of parameters that can be used when defining common methods, such as default parameters, keyword parameters, and so on, can be used for the definition of block variables and are assigned to the Proc#call method.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.