#当前块
class Block
def a_method
return yield if block_given?
'no block'
end
end
obj=Block.new
puts "#{obj.a_method}"
puts "#{obj.a_method {"here's a block!"}}"
 #闭包
class Greeter
def initialize(name)
@name=name
end
def name
@name
end
def name=(new_name)
@name=new_name
end
end
g=Greeter.new("Barney")
puts g.name     
g.name="Betty"
puts g.name      
 #切换作用域
v1=1
class MyClass
v2=2
local_variables
#puts "#{local_variables}"   #[:v2]  
def my_method
v3=3
local_variables
#puts "#{local_variables}" #[:v3]
end
local_variables
#puts "#{local_variables}" #[:v2] end
obj=MyClass.new
obj.my_method #[:v3]
puts "#{local_variables}" #[:v1, :obj]

ruby学习--block的更多相关文章

  1. Ruby学习笔记4: 动态web app的建立

    Ruby学习笔记4: 动态web app的建立 We will first build the Categories page. This page contains topics like Art, ...

  2. ruby 学习笔记 1

    写ruby blog  系统的记录下.也是对我学ruby的点滴记录. 先介绍下我的学习环境.系统:ubuntu12.04文档:techotopia ,ruby文档,the hard way learn ...

  3. Ruby学习心得之 Linux下搭建Ruby环境

    作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Ruby学习心得之 Linux下搭建Ruby环境1.前言2.Linux下安装Ruby环境 一 ...

  4. Ruby学习之mixin

    直接上代码: module Action def jump @distance = rand(4) + 2 puts "I jumped forward #{@distance} feet! ...

  5. ruby学习网站

    Ruby官方中文网(推荐): https://www.ruby-lang.org/zh_cn/ 国内非常不错的Ruby学习教程网站(推荐): http://www.yiibai.com/ruby Ru ...

  6. ruby学习笔记(1)-puts,p,print的区别

    ruby学习笔记-puts,p,print的区别 共同点:都是用来屏幕输出的. 不同点:puts 输出内容后,会自动换行(如果内容参数为空,则仅输出一个换行符号):另外如果内容参数中有转义符,输出时将 ...

  7. Ruby学习之代码块

    代码块在其他的语言中都或多或少接触过一些,如perl中sort{$a<=>$b}keys,传入代码块实现按数值排序,在swift中用到闭包,更加深入学习到training closure. ...

  8. Ruby中Block, Proc, 和Lambda

    Block Blocks就是存放一些可以被执行的代码的块,通常用do...end 或者 {}表示 例如: [1, 2, 3].each do |num| puts num end [1, 2, 3]. ...

  9. Ruby学习资源汇总

    from:http://segmentfault.com/a/1190000000362058 Ruby 语言 Try Ruby: 无需在你的系统中安装.Ruby,只要通过浏览器便可立即体验 Ruby ...

随机推荐

  1. Django官方文档学习1——第一个helloworld页面

    Django 1.10官方文档:https://docs.djangoproject.com/en/1.10/intro/tutorial01/ 1.查看django版本 python -m djan ...

  2. 用CToolBarCtrl类为对话框创建工具栏

    ---恢复内容开始--- 首先CToolBarCtrl类内部维护了三个重要的数据结构:一个图像列表,一个字符串列表,一个TBBUTTON结构体的列表. 知道了这一点,下面的理解起来就轻松了.慢慢来: ...

  3. CMSIS Example - osTimer osTimerCreate osTimerStart

    osTimerId timer; uint32_t cnt=; void timerHandler( void * arg ) { cnt++; osTimerStart( timer, ); } o ...

  4. Semaphore 和 Mutex

    mutex和semaphore有什么区别呢? mutex是用作互斥的,而semaphore是用作同步的. 也就是说,mutex的初始化一定是为1,而semaphore可以是任意的数, 所以如果使用mu ...

  5. Codeforces Gym 100015C City Driving 离线LCA

    City Driving 题目连接: http://codeforces.com/gym/100015/attachments Description You recently started fre ...

  6. shader 语言 【转】

    shader语言 3dlabs改名后其开发者网站关闭 可以在这里下载shadergen http://3dshaders.com/home/index.php?option=com_weblinks& ...

  7. pylons使用多个数据库(multiple DB)

    最近做的工程要修改成两个数据库的,一个测试数据库, 一个线上数据库. 所以就要把原来的只有一个数据库的改成两个数据库. 第一步:修改development.ini # SQLAlchemy datab ...

  8. Android端百度地图API使用详解

    百度地图API简介 百度地图移动版API(Android)是一套基于Android设备的应用程序接口,通过该接口,可以轻松的访问百度服务和数据,构建功能丰富.交互性强的地图应用程序. 百度地图移动版A ...

  9. 对cocos2d 之autorelease\ratain\release的理解

    前言: 三种情况,引出问题     new出来的对象需要释放,而释放时,如果有其他人引用了这个对象,再次使用这个对象时,则会导致无效指针报错.     于是有了引用计数的施放管理机制.       对 ...

  10. Java IO和Java NIO在文件拷贝上的性能差异分析

    1.  在JAVA传统的IO系统中,读取磁盘文件数据的过程如下: 以FileInputStream类为例,该类有一个read(byte b[])方法,byte b[]是我们要存储读取到用户空间的缓冲区 ...