ruby学习--block
#当前块
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的更多相关文章
- Ruby学习笔记4: 动态web app的建立
Ruby学习笔记4: 动态web app的建立 We will first build the Categories page. This page contains topics like Art, ...
- ruby 学习笔记 1
写ruby blog 系统的记录下.也是对我学ruby的点滴记录. 先介绍下我的学习环境.系统:ubuntu12.04文档:techotopia ,ruby文档,the hard way learn ...
- Ruby学习心得之 Linux下搭建Ruby环境
作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Ruby学习心得之 Linux下搭建Ruby环境1.前言2.Linux下安装Ruby环境 一 ...
- Ruby学习之mixin
直接上代码: module Action def jump @distance = rand(4) + 2 puts "I jumped forward #{@distance} feet! ...
- ruby学习网站
Ruby官方中文网(推荐): https://www.ruby-lang.org/zh_cn/ 国内非常不错的Ruby学习教程网站(推荐): http://www.yiibai.com/ruby Ru ...
- ruby学习笔记(1)-puts,p,print的区别
ruby学习笔记-puts,p,print的区别 共同点:都是用来屏幕输出的. 不同点:puts 输出内容后,会自动换行(如果内容参数为空,则仅输出一个换行符号):另外如果内容参数中有转义符,输出时将 ...
- Ruby学习之代码块
代码块在其他的语言中都或多或少接触过一些,如perl中sort{$a<=>$b}keys,传入代码块实现按数值排序,在swift中用到闭包,更加深入学习到training closure. ...
- Ruby中Block, Proc, 和Lambda
Block Blocks就是存放一些可以被执行的代码的块,通常用do...end 或者 {}表示 例如: [1, 2, 3].each do |num| puts num end [1, 2, 3]. ...
- Ruby学习资源汇总
from:http://segmentfault.com/a/1190000000362058 Ruby 语言 Try Ruby: 无需在你的系统中安装.Ruby,只要通过浏览器便可立即体验 Ruby ...
随机推荐
- VPW Communication Protocol
http://www.fastfieros.com/tech/vpw_communication_protocol.htm Breakdown of the j1850 3 byte Header f ...
- CSS规则整理
一. 善用css缩写规则 /*注意上.右.下.左的书写顺序*/1. 关于边距(4边):1px 2px 3px 4px (上.右.下.左)1px 2px 3px (省略的左等于右)1px 2px (省略 ...
- SQL扫描并执行文件夹里的sql脚本
场景:项目数据库操作全部使用存储过程实现.每天都会有很多存储过程更新/增加,人工对测试环境中存储过程更新,会有一定概率出现遗漏,也麻烦!所以,需要一个工具将文件夹中所有存 储过程执行一 ...
- MySQL中ON DUPLICATE KEY UPDATE使用
今天做推断插入用到了MySQL中ON DUPLICATE KEY UPDATE,如今Mark下面! 假设你想做到数据库中没有数据的话插入数据.有数据的话更新数据,那么你能够选择ON DUPLICATE ...
- UVALive 4223 Trucking 二分+spfa
Trucking 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...
- 【MongoDB】mongoimport and mongoexport of data (一)
In the software development, we usually are faced with a common question of exporting or importing d ...
- 【JavsScript】一个元素绑定多个事件
<script> document.getElementById("a").addEventListener("click", function() ...
- VS2010中文/vs2008英文版/vs2005下载地址
ed2k://|file|cn_visual_studio_2010_ultimate_x86_dvd_532347.iso|2685982720|4AE6228933DDE49D9BFA4C3467 ...
- 40多个关于人脸检测/识别的API、库和软件
英文原文:List of 40+ Face Detection / Recognition APIs, libraries, and software 译者:@吕抒真 译文:链接 自从谷歌眼镜被推出以 ...
- RosettaNet
RosettaNet 这一名字源自于 1799 年在埃及发现的 Rosetta Stone .这要追溯到公元前 196 年,该石头是在 Rosetta (Rashid) 镇附近被人发现的,上面用两种不 ...