---
title: "ruby学习笔记--Blocks"
author: "minstrel"
date: "2007-12-27T20:52:34Z"
tags: []
slug: "ruby-blocks"
draft: false
lastmod: "2007-12-27T20:52:34Z"
id: 151337
url: "https://minstrel.iteye.com/blog/151337"
---

Proc的两个使用方法

ruby 代码

1. def some\_mtd1 aproc
2. aproc.call
3. end
4. some\_mtd1 lambda { puts "aaaaa" }

 这个代码等同于下面这个代码段

ruby 代码

1. def some\_mtd2 &bproc
2. bproc.call
3. end
4. some\_mtd2 { puts "aaaaa" }

 也等同于下面代码

ruby 代码

1. ab = lambda { |x| puts x }
2. ab.call 'aaaaa'

c = lambda { |i| puts i }  
c = Proc.new { |i| puts i }  
c = proc { |i| puts i } 

The above 3 statements do the same thing: instantiate a block object. ‘proc’ is an alias for ‘**lambda**‘ and they work slightly different than ‘**Proc.new**‘. In Ruby 1.9, ‘proc’ will probably be an alias for ‘**Proc.new**‘ instead.