ruby practice
来源:互联网 发布:python 列表中的元组 编辑:程序博客网 时间:2024/05/18 10:32
classAmbiguous
defx
1
end
deftest
putsx
x= 0 if false
putsx
x= 2
putsx
end
end
am= Ambiguous.new
am.test
#! /usr/bin/ruby##我们首先用“catch(:done)”标注了一个块,#当执行到“throw :done”时,中止当前“catch(:done)”所标注的块,#处理流程继续向下处理。 在catch/throw中,当碰到throw时,#Ruby会展开调用堆栈来查找匹配的catch,#当找到后,堆栈会被展开,catch块剩余的代码不会被执行。 #Ruby中没有goto 语句,但你可以使用catch/throw或异常来实现goto的功能。 #def m(n) puts n throw :done unless n > 0 m(n-1)endcatch(:done) { m(3) puts "Cannot reach here!"}puts "Reach here!"
class FixedStack < Stack def initialize(limit) super() @limit = limit end def push(val) if @sp >= @limit puts "Over limit" return end super(val) end def top return @stack[-1] endend
require "observer"
classTick
includeObservable
deftick
loopdo
now= Time.now
ifnow.sec[0]!= 1
changed
end
notify_observers(now.hour,now.min,now.sec)
sleep1.0 - Time.now.usec/ 1000000.0
end
end
end
classTextClock
defmy_update(h,m, s)
printf"\e[8D%02d:%02d:%02d",h, m,s
STDOUT.flush
end
end
if$0 == __FILE__
tick= Tick.new
tick.add_observer(TextClock.new,:my_update)
tick.tick
end
#! /usr/bin/ruby
defadd_gram_to_multi_word(token)
pron= token.split("-").collect{| w | w.chomp.strip}.join(" ")
stories= Dir.glob('stories/*')
stories.eachdo |story|
story_changed= false
File.openstory do |file|
arr= file.collectdo |line|
ifline =~ /<w .* t="#{token}" .*\/>/
gram= "<gram t=\"#{pron}\" />"
putsline
story_changed= true
num_of_white_space= line.index(/<w/)
putsnum_of_white_space
prefix_t= " " * (num_of_white_space + 4)
prefix_w= " " * num_of_white_space
line.gsub!(/\s*\/>/,">")
gram= prefix_t +gram + "\n"
sub_w= prefix_w +"</w>\n"
putsline + gram + sub_w
line+ gram + sub_w
else
line
end
end
ifstory_changed
File.open(story,"w") do |f|
arr.eachdo |a|
f.write(a)
end
end
end
end
end
end
token_list= ["multi-vitamin"]
token_list.eachdo |token|
add_gram_to_multi_wordtoken
end
module Seq
defself.fromtoby(from,to, by)
x= from
whilex <= to
yieldx
x+= by
end
end
end
if__FILE__ == $0
Seq.fromtoby(1,10, 2){|x|puts x}
end
require "builder/xmlmarkup"
xml= Builder::XmlMarkup.new(:indent=> 2)
putsxml.html {
xml.head{
xml.title("History")
}
xml.body{
xml.h1("Header")
xml.p{
xml.text!("paragraph with")
xml.a("a Link","href" => "http://onestepback.org")
}
}
}
class Sequence def initialize(from, to, by) @from, @to, @by = from, to, by end def each x = @from while x <= @to yield x x = x + @by end end def length return 0 if @to < @from Integer((@to - @from)/@by) + 1 end def [](index) return nil if index < 0 v = @from + index*@by if v <= @to v else nil end end def *(factor) Sequence.new(@from*factor, @to*factor, @by*factor) end def +(offset) Sequence.new(@from + offset, @by + offset, @by) endendif __FILE__ == $0 s = Sequence.new(1, 10, 2) s.each {|x| puts x}end
- ruby practice
- practice
- practice
- ruby
- ruby
- ruby
- Ruby
- Ruby
- Ruby
- ruby
- ruby
- ruby
- Ruby
- RUBY
- ruby
- Ruby
- ruby
- Ruby
- linux sqlplus Backspace 乱码 与上下翻看功能设置
- 【数据库】mysqldump中使用flush tables with read lock的风险分析
- DICOM标准
- 浅谈WebKit之Port篇
- 防止poll监听被中断
- ruby practice
- Oracle 编写函数01
- centos 中 APC for php 部署配置简解
- iOS: iOS5新特性 ARC
- Android JNI开发入门篇
- Android动画Animation的使用详解
- kill掉android应用的bat
- Java文件操作的基本概念和简单操作
- hibernate 映射文件java与sql类型对照