1、map的声明方法:
m={"1"=>"3","2"=>"aa"}
2、if count>10
....
elsif c<3
else
...
end
3、while c<3
...
end
4、正则表示式方法:
/d3/
有match等方法
判断字符串的匹配:
if line=~/Perl|Python/
...
替换:
line.sub(/Perl/,'Ruby')将第一个Perl替换成Ruby
line.gsub(/Perl/,'Ruby')将所有Perl替换成Ruby
5、block的使用:
as = ["2","66"]
as.each{|a|print a}
6、变量的作用域:@成员变量,@@类变量,$全局变量
7、
class Person
def initialize(name,age)
@name = name
@age = age
end
def say()
print "我叫"+@name
print "#@age"
end
end
p = Person.new("tom",20)
p.say()
print p.inspect#反射显示对象的内容
8、类方法与类变量:
class Person
@@count = 0
def Person.doIt
end
end
9、数组的子数组:
a=[1,2,3,5]
a[1..3]→[2,3,5]
切片
a=[1,2,3,5]
a[0..2]=[4]
a→[4,5]
10、for语句:
for i in 0..100
print i
end
11、block:
def a
for i in 0..100
yield i
end
end
a{|f|puts f}
带返回值的block:
def a
for i in 1..100
if yield i
puts "接受"+i.to_s
end
end
end
a{|v|v>90}
应用强大的block:
m={}
(1..100).each{|a| m[a]=a**a}
m.each{|a|puts a}
12、block与变长参数:
class FileProcess
def FileProcess.process(*args)
f = File.open(*args)
yield f
f.close()
end
end
FileProcess.process("c:/boot.ini","r"){
|file|
while line=file.gets
puts line
end
}
另一种用法:
FileProcess.process("c:/boot.ini","r") do |file|
while line=file.gets
puts line
end
end
13、闭包:
class MyClass
def initialize(&action)
@action = action
end
def begin()
for i in 1..100
if i>90
@action.call(self)
end
end
end
end
a = MyClass.new(){puts "超限"}
a.begin()
lambda:
a = lambda{|n| puts n}
a.call(3)
14 数字:
6.times{|i|puts i}
1.upto(5){|i| puts i}
50.step(100,10){|i| puts i}
15 转化
a="33"
b="55"
puts Integer(a)+Integer(b)
puts a.to_i+b.to_i
puts a*3
16 字符串中内嵌ruby表达式
puts "He#{'l'*100}o"
puts "1+1=#{1+1}"
city = "peking"
y=2008
puts "#{city}-#{y}"
注意只有用双引号括起来的才支持内嵌表达式
17 puts "2005|03|08".split(/\|/)
18 Range
('a'..'f').each{|c| puts c}
Range转化为数组:puts ('a1'..'a9').to_a
19 返回bool的方法通常以?结尾,修改对象状态的方法以!结尾
20 自定义复数:
class FuNum
def initialize(shi,xu)
@shi=shi
@xu=xu
end
def xu
@xu
end
def shi
@shi
end
public :shi,:xu
def +(other)
newshi = @shi+other.shi
newxu = @xu+other.xu
return FuNum.new(newshi,newxu)
end
def to_s()
return "#{@shi}+#{@xu}i"
end
end
i = FuNum.new(1,2)
j = FuNum.new(2,9)
puts i+j
21 交换两个变量(并行赋值):
a=20
b=30
c=40
a,b,c=c,b,a
print a,b,c
python也支持
22 Case语句:
a=1
salary=case a
when 0..100 then 3000
when 101 then 333
else 888
end
puts salary
Case语句的When部分还支持正则表达式
case line
when /title=(. )/
puts"Titleis#$1"
when/track=(. )/
puts"Trackis#$1"
when/artist=(. )/
puts"Artistis#$1"
end
23 异常处理
begin
raise "33333333"
rescue RuntimeError
#$!表示异常信息
print $!
#再次抛出
raise
end
begin
raise "33333333"
rescue RuntimeError
#$!表示异常信息
print $!
#再次抛出
ensure
print "无论如何都被打印"
end
还可以在rescue中调用retry来重新执行程序块
24 模块
CowNew.rb模块文件
module CowNew
PI = 3.14
def CowNew.calc(r)
return 2*PI*r
end
end
调用者Main.rb
require "CowNew"
puts CowNew.calc(3)
从文件加载模块:
load "E:/greeninst/eclipse3.2.2/eclipse/workspace/r/CowNew.rb"
25 文件处理:
f = "E:/greeninst/eclipse3.2.2/eclipse/workspace/r/CowNew.rb"
puts IO.read(f)
puts IO.readlines(f)
逐行处理:
f = "E:/greeninst/eclipse3.2.2/eclipse/workspace/r/CowNew.rb"
i=IO.foreach(f){|line| puts line}
f = "E:/greeninst/eclipse3.2.2/eclipse/workspace/r/CowNew.rb"
i=0
IO.foreach(f) do |line|
i=i+1
print i," ",line
end
26 流:STDOUT<<33<<"adfa"
27 Ruby专用Http服务器,支持eruby,免得使用Apache,调试方便:
require "webrick"
include WEBrick
server = HTTPServer.new(:DocumentRoot=>"E:/俺的文档/个人资料/网站安全/cownew空间/")
server.start()
在目录web下创建rhtml文件,增加服务器:
require "webrick"
include WEBrick
server = HTTPServer.new(:DocumentRoot=>File.join(Dir.pwd,"web"))
server.start()
自定义Servlet:
require "webrick"
include WEBrick
class HelloServlet<HTTPServlet::AbstractServlet
def do_GET(req,res)
res["Content-Type"] = "text/html"
res.body="aaaaaaaaa"
end
end
server = HTTPServer.new(:DocumentRoot=>File.join(Dir.pwd,"web"))
server.mount("/hello",HelloServlet)
server.start()