#当前块
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. Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones 水题

    A. Case of the Zeros and Ones Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/con ...

  2. 局域网两台笔记本如何使用svn

    前几天我要和朋友一起开发一个网站,但是都是两台笔记本,连局域网搞的很麻烦,后来就用了git,今天突然想到要用svn,就在网上找了这个办法,结果一试便可以了,很开心 很感谢楼主,下面是我做的步骤绝对给力 ...

  3. [NOIP 2014复习]第三章:动态规划——NOIP历届真题回想

    背包型动态规划 1.Wikioi 1047 邮票面值设计 题目描写叙述 Description 给定一个信封,最多仅仅同意粘贴N张邮票,计算在给定K(N+K≤40)种邮票的情况下(假定全部的邮票数量都 ...

  4. 《Linux命令行与shell脚本编程大全》 第二十二章 学习笔记

    第二十二章:使用其他shell 什么是dash shell Debian的dash shell是ash shell的直系后代,ash shell是Unix系统上原来地Bourne shell的简化版本 ...

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

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

  6. Golang学习 - strconv 包

    ------------------------------------------------------------ // 将布尔值转换为字符串 true 或 false func FormatB ...

  7. mysql describe

    describe命令一.describe命令用于查看特定表的详细设计信息,例如为了查看guestbook表的设计信息,可用:describe guestbook describe ol_user us ...

  8. Google搜索技巧-从入门到精通(从此学习进步、工作顺心)

    转载:http://www.blogbus.com/koudaizhi-logs/55687286.html 一  GOOGLE简介 Google (www.google.com)是一个搜寻引擎,由某 ...

  9. 琐碎-hadoop2.2.0-hbase0.96.0-hive0.13.1整合

    关于hadoop和hive.hbase的整合就不说了,这里就是在hadoop2.2.0的环境下整合hbase和hive 因为hive0.12不支持hadoop2,所以还要替换一些hadoop的jar包 ...

  10. org.apache.hadoop.conf-Configured

    org.apache.hadoop.conf中的最后一个类,也是这个包中以后用的最频繁的一个,Configurable算是肉体,Configuration算是灵魂吧 package org.apach ...