#当前块
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. VNC 抓取远程桌面

    VNC (Virtual Network Computing)是虚拟网络计算机的缩写.VNC 是一款优秀的远程控制工具软件,由著名的 AT&T 的欧洲研究实验室开发的.VNC 是在基于 UNI ...

  2. Binary Search

    Binary Search                              [原文见:http://www.topcoder.com/tc?module=Static&d1=tuto ...

  3. scenes & segues within storyboards

    Scenes Scenes in a storyboard represent content shown within one screen in your application. A scene ...

  4. IOS 7 Study - Implementing Navigation with UINavigationController

    ProblemYou would like to allow your users to move from one view controller to the other witha smooth ...

  5. Codeforces Good bye 2015 B. New Year and Old Property dfs 数位DP

    B. New Year and Old Property 题目连接: http://www.codeforces.com/contest/611/problem/B Description The y ...

  6. cdoj 题目简单分类

    如有错误请联系我,下面题的题解,除了傻逼题均可以在我blog中查找 1 傻逼题 3 傻逼题 4 傻逼题 15 dfs 24 傻逼题 25傻逼题 26 傻逼题 30 flyod 31 01背包 42 k ...

  7. Codeforces Round #310 (Div. 2) B. Case of Fake Numbers 水题

    B. Case of Fake Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  8. ANativeWindow是个什么东西

    公司经常组织一些培训,培训的都是些奇技淫巧.什么设计模式啦,开发策略啦,git啦,repo啦,另外就是培训一些开发流程的东东,例如CMMI啦.可是,却忘记了,程序员终究要归结到三个问题上: 1.解决什 ...

  9. java 图的邻接矩阵

    有向图 在有向图中,结点对<x ,y>是有序的,结点对<x,y>称为从结点x到结点y的一条有向边,因此,<x,y>与<y,x>是两条不同的边.有向图中的 ...

  10. C# 中传递多个参数给多线程

    1.方式一:使用ParameterizedThreadStart委托 如果使用了ParameterizedThreadStart委托,线程的入口必须有一个object类型的参数,且返回类型为void. ...