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()