易截截图软件、单文件、免安装、纯绿色、仅160KB

Use lambda in Ruby 九筒一条

http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
Understanding Ruby Blocks, Procs and Lambdas
Blocks, Procs and lambdas (referred to as closures
in Computer Science) are one of the most powerful aspects of Ruby, and
also one of the most misunderstood. This is probably because Ruby
handles closures in a rather unique way. Making things more complicated
is that Ruby has four different ways of using closures, each of which is
a tad bit different, and sometimes nonsensical. There are quite a few
sites with some very good information about how closures work within
Ruby. But I have yet to find a good, definitive guide out there.
Hopefully, this tutorial becomes just that.
First Up, Blocks
The most common, easiest and arguably most “Ruby like” way to use
closures in Ruby is with blocks. They have the following familiar
syntax:
array
=
[
1
,
2
,
3
,
4
]

array
.
collect!
do
|
n
|
n
**
2
end
puts
array
.
inspect
# => [1, 4, 9, 16]
So, what is going on here?
First, we send the collect!
method to an Array with a
block of code.
The code block interacts with a variable used within the collect!
method (n
in this case) and squares it.
Each element inside the array is now squared.
Using a block with the collect!
method is pretty easy,
we just have to think that collect!
will use the code
provided within the block on each element in the array. However, what
if we want to make our own collect!
method? What will it
look like? Well, lets build a method called iterate!
and
see.
class
Array
def
iterate!
self
.
each_with_index
do
|
n
,
i
|
self
[
i
]
=
yield
(
n
)
end
end
end
array
=
[
1
,
2
,
3
,
4
]
array
.
iterate!
do
|
n
|
n
**
2
end
puts
array
.
inspect
# => [1, 4, 9, 16]
To start off, we re-opened the Array class and put our iterate!
metho


相关文档:

Ruby on Rails中select使用方法

在Ruby on Rails中真的有一堆Select helper可以用,我们经常容易混淆。常见的有三个..
select, select_tag, collection_select(其余的什么select_date那些不谈)
我们先来看看一个基本的下拉式选项骨架
</p>
<select
name="ROR">
<option
value="1">ROR1</option><br
/>
<optio ......

Windows平台的ruby IDE 点评

转自 http://www.javaeye.com/topic/57474 
Windows平台的ruby IDE 点评
在MacOS平台几乎没有什么争议性,大家都用TextMate。但是Windows平台可供选择和使用的IDE很多,却各有各的长处和短处。基于我用过的所有ruby IDE点评一下。windows平台的RoR IDE主要分为两类:一类是重量级的全功能IDE,例如Eclipse,Netbeans ......

ruby调用平台指令删除指定文件夹

正常情况下,ruby如果发现文件夹中有system,readonly,hide类型的文件,则是不能直接删除
他们的。如何解决呢?
以其人之道还治其人之身!
def dryf(fpath) #destroy file
x = Iconv.iconv('utf-8','gbk',fpath).to_s
%x{attrib -R -S -H \"#{Iconv.iconv('gbk','utf-8',x).to_s}\"}
f = File.new(f ......

ruby来枚举csdn未注册的用户名

require 'open-uri'
$NAME_CHARS= (?a..?z).to_a+(?0..?9).to_a
def is_name_used(name)
str=open('http://passport.csdn.net/UserExist.aspx?UserName='+name)
str=str.read
#str=str.encode('GBK','utf-8')
return true if str[/Red/]
end
def enum_names(len=2)
return if len<2
f=File.open(' ......

ruby随机在控制台显示字符串

require 'curses'
module Curses
def self.program
main_scr=init_screen
noecho
cbreak
curs_set(0)
main_scr.keypad=true
yield main_scr
end
end
Curses.program do |scr|
max_x=scr.maxx
max_y=scr.maxy
100.times do
scr.setpos(rand(max_y),rand(max_x))
......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号