如何sql中使用splitRuby中的“split”方法

ruby文件操作 - notreally - ITeye博客
博客分类:
转!1 检测文件是否存在及其大小 FileTest的 exist?方法可以检测一个文件是否存在: Ruby代码 flag = FileTest::exist?("LochNessMonster")
flag = FileTest::exists?("UFO")
flag = FileTest::exist?("LochNessMonster")
flag = FileTest::exists?("UFO")
# exists? is a synonym for exist?如果我们想要知道文件是否有内容,可以使用File::Stat的zero? 方法: Ruby代码 flag = File.new("somefile").stat.zero?
flag = File.new("somefile").stat.zero?这个将会返回true,这是因为在ruby中0也是true,nil才是false. 所以我们可以使用size?方法: Ruby代码 if File.new("myfile").stat.size?
puts "The file has contents."
puts "The file is empty."
if File.new("myfile").stat.size?
puts "The file has contents."
puts "The file is empty."
endFileTest模块里面也有zero? 和size?方法: Ruby代码 flag1 = FileTest::zero?("file1")
flag2 = FileTest::size?("file2")
flag1 = FileTest::zero?("file1")
flag2 = FileTest::size?("file2")这里还有一个size方法: Ruby代码 size1 = File.size("file1")
size2 = File.stat("file2").size
size1 = File.size("file1")
size2 = File.stat("file2").size
2 检测特殊文件属性 这边要注意,File类mix了FIleTest模块,并且FileTest模块和File::Stat模块功能上也有很多重复. unix/linux有面向字符和面向块的设备。FileTest的方法blockdev?和chardev?可以进行测试: Ruby代码 flag1 = FileTest::chardev?("/dev/hdisk0")
flag2 = FileTest::blockdev?("/dev/hdisk0")
flag1 = FileTest::chardev?("/dev/hdisk0")
flag2 = FileTest::blockdev?("/dev/hdisk0") # true有时我们想要知道一个流是否联系到了终端,这时我们可以使用IO类的tty?方法: Ruby代码 flag1 = STDIN.tty?
flag2 = File.new("diskfile").isatty
flag1 = STDIN.tty?
flag2 = File.new("diskfile").isatty
# false一个流可以是一个管道,或者一个socket: Ruby代码 flag1 = FileTest::pipe?(myfile)
flag2 = FileTest::socket?(myfile)
flag1 = FileTest::pipe?(myfile)
flag2 = FileTest::socket?(myfile)要区分目录和普通文件我们这样使用: Ruby代码 file1 = File.new("/tmp")
file2 = File.new("/tmp/myfile")
test1 = file1.directory?
test2 = file1.file?
test3 = file2.directory?
test4 = file2.file?
file1 = File.new("/tmp")
file2 = File.new("/tmp/myfile")
test1 = file1.directory?
test2 = file1.file?
test3 = file2.directory?
test4 = file2.file?
# trueFile还有一个类方法ftype,他将返回流的类型.他也在File::Stat里面,只不过是实例方法.它的返回值可能是下面的字符 串(file、directory、blockSpecial、characterSpecial、fifo、link或socket). Ruby代码 this_kind = File.ftype("/dev/hdisk0")
that_kind = File.new("/tmp").stat.ftype
this_kind = File.ftype("/dev/hdisk0")
# "blockSpecial"
that_kind = File.new("/tmp").stat.ftype
# "directory"要测试一个文件是否为另一个文件的链接,可以使用FileTest的symlink?方法,要计算链接数量,可以使用nlink方法: Ruby代码 File.symlink("yourfile","myfile")
is_sym = FileTest::symlink?("myfile")
hard_count = File.new("myfile").stat.nlink
File.symlink("yourfile","myfile")
# Make a link
is_sym = FileTest::symlink?("myfile")
hard_count = File.new("myfile").stat.nlink
# 03 使用管道 ruby中使用IO.popen打开管道: Ruby代码 check = IO.popen("spell","r+")
check.puts("'T was brillig, and the slithy toves")
check.puts("Did gyre and gimble in the wabe.")
check.close_write
list = check.readlines
list.collect! { |x| x.chomp }
check = IO.popen("spell","r+")
check.puts("'T was brillig, and the slithy toves")
check.puts("Did gyre and gimble in the wabe.")
check.close_write
list = check.readlines
list.collect! { |x| x.chomp }
# list is now %w[brillig gimble gyre slithy toves wabe]要注意 必须调用close_write,如果没有调用它,读取管道的时候,就不能到达文件的末尾. 下面是一个block的形式: Ruby代码 File.popen("/usr/games/fortune") do |pipe|
quote = pipe.gets
puts quote
File.popen("/usr/games/fortune") do |pipe|
quote = pipe.gets
puts quote
# On a clean disk, you can seek forever. - Thomas Steel
end如果指定了一个字符串"-",那么一个新的ruby实例将被创建.如果指定了一个block,那么这个block将会作为两个独立 的进程运行。子进程得到nil,父进程得到一个IO对象: Ruby代码 IO.popen("-") do |mypipe|
puts "I'm the parent: pid = #{Process.pid}"
listen = mypipe.gets
puts listen
puts "I'm the child: pid = #{Process.pid}"
IO.popen("-") do |mypipe|
puts "I'm the parent: pid = #{Process.pid}"
listen = mypipe.gets
puts listen
puts "I'm the child: pid = #{Process.pid}"
I'm the parent: pid = 10580
I'm the child: pid = 10582pipe方法也返回互相连接的一对管道: Ruby代码 pipe = IO.pipe
reader = pipe[0]
writer = pipe[1]
thread1 = Thread.new(reader,writer) do |reader,writer|
str = reader.gets
reader.close
thread2 = Thread.new(reader,writer) do |reader,writer|
writer.puts("What hath God wrought?")
writer.close
thread1.join
thread2.join
pipe = IO.pipe
reader = pipe[0]
writer = pipe[1]
thread1 = Thread.new(reader,writer) do |reader,writer|
# writer.close_write
str = reader.gets
reader.close
thread2 = Thread.new(reader,writer) do |reader,writer|
# reader.close_read
writer.puts("What hath God wrought?")
writer.close
thread1.join
thread2.join
# What hath God wrought?4 使用非阻塞IO ruby会在后台执行一些操作,使io不会被阻断,因此大部分情况下可以使用ruby线程来管理IO,当一个线程被Io阻塞之 后,另外的线程能够继续执行. 由于ruby的线程不是一个native的线程,因此ruby的线程都在同一个进程里面. 如果你想关闭一个非阻塞io,你可以这样做: Ruby代码 require 'io/nonblock'
test = mysock.nonblock?
mysock.nonblock = true
mysock.nonblock = false
mysock.nonblock { some_operation(mysock) }
mysock.nonblock(false) { other_operation(mysock) }
require 'io/nonblock'
test = mysock.nonblock?
mysock.nonblock = true
# turn off blocking
mysock.nonblock = false
# turn on again
mysock.nonblock { some_operation(mysock) }
# Perform some_operation with nonblocking set to true
mysock.nonblock(false) { other_operation(mysock) }
# Perform other_operation with non-blocking set to false5 使用readpartial readpartial被设计来用于就像socket这样的流. readpartial要求提供最大长度的参数,如果指定了buffer,那么这个buffer应指向用于存储数据的一个字符串。 Ruby代码 data = sock.readpartial(128)
data = sock.readpartial(128)
# Read at most 128 bytesreadpartial 方法,不能接受非阻塞的flag,他有时会阻塞:IO对象的buffer是空的;流的内容为空;流没有到达文件末尾 。 因此,如果流中还有数据的话,readpartial将不会阻塞. 如果流没有数据,并且他已经抵达文件的末尾,readpartial 将会立即抛出一个EOFError. 如果调用阻塞,他将会等待直到接收到数据或者得到一个EOF. 当sysread 调用在阻塞模式下,他的行为与readpartial相似. 6 操作路径名 先来看一下File.dirname和File.basename方法: Ruby代码 str = "/home/dave/podbay.rb"
dir = File.dirname(str)
file1 = File.basename(str)
file2 = File.basename(str,".rb")
str = "/home/dave/podbay.rb"
dir = File.dirname(str)
# "/home/dave"
file1 = File.basename(str)
# "podbay.rb"
file2 = File.basename(str,".rb")
# "podbay"File.split方法,可以将一个文件的路径名和文件名分隔开: Ruby代码 info = File.split(str)
info = File.split(str)
# ["/home/dave","podbay.rb"]类方法expand_path 将一个相对路径,转换为一个绝对路径名: Ruby代码 Dir.chdir("/home/poole/personal/docs")
abs = File.expand_path("../../misc")
Dir.chdir("/home/poole/personal/docs")
abs = File.expand_path("../../misc")
# "/home/poole/misc"对于打开的文件,path 将会返回这个文件的路径名: Ruby代码 file = File.new("../../foobar")
name = file.path
file = File.new("../../foobar")
name = file.path
# "../../foobar"类方法类方法join正好和split相反: Ruby代码 path = File.join("usr","local","bin","someprog")
path = File.join("usr","local","bin","someprog")7使用Pathname pathname类实际上是,Dir, File, FileTest,和FileUtils的包装器,它包含他们的很多功能: Ruby代码 require 'pathname'
path = Pathname.new("home/hal")
file = Pathname.new("file.txt")
p2 = path + file
path.directory?
path.file?
p2.directory?
puts parts = p2.split
puts ext = p2.extname
require 'pathname'
path = Pathname.new("home/hal")
file = Pathname.new("file.txt")
p2 = path + file
path.directory?
path.file?
p2.directory?
puts parts = p2.split
# [Pathname:/home/hal, Pathname:file.txt]
puts ext = p2.extname
# .txt再看看其他的有用的方法: Ruby代码 p1 = Pathname.new("//")
p2 = Pathname.new("/home/poole")
p3 = p2.parent
items = p2.children
p1 = Pathname.new("//")
# odd but legal
p2 = Pathname.new("/home/poole")
p3 = p2.parent
# Pathname:/home
items = p2.children
# array of Pathnames (all files and
# dirs immediately under poole)relative和absolute判断路径是否是相对的: Ruby代码 p1 = Pathname.new("/home/dave")
p1.absolute?
p1.relative?
p1 = Pathname.new("/home/dave")
p1.absolute?
p1.relative?
# false8 Command-Level 文件操作 其实也就是copy, delete, rename,等等 些操作了: Ruby代码 File.delete("history")
File.unlink("toast")
File.rename("Ceylon","SriLanka")
File.link("/etc/hosts","/etc/hostfile")
File.symlink("/etc/hosts","/tmp/hosts")
File.truncate("myfile",1000)
File.delete("history")
File.unlink("toast")
File.rename("Ceylon","SriLanka")
File.link("/etc/hosts","/etc/hostfile")
# hard link
File.symlink("/etc/hosts","/tmp/hosts")
# symbolic link
File.truncate("myfile",1000)
# Now at most 1000 bytesfileUtils也有很多有用的方法 Ruby代码 require "fileutils"
same = pare_file("alpha","beta")
FileUtils.copy("epsilon","theta", true)
FileUtils.move("/tmp/names","/etc")
FileUtils.move("colours","colors")
FileUtils.safe_unlink("alpha","beta","gamma")
FileUtils.safe_unlink("delta","epsilon",true)
FileUtils.install("foo.so","/usr/lib")
require "fileutils"
same = pare_file("alpha","beta")
# Copy epsilon to theta and log any errors.
FileUtils.copy("epsilon","theta", true)
FileUtils.move("/tmp/names","/etc")
# Move to new directory
FileUtils.move("colours","colors")
# Just a rename
FileUtils.safe_unlink("alpha","beta","gamma")
# Log errors on the next two files
FileUtils.safe_unlink("delta","epsilon",true)
FileUtils.install("foo.so","/usr/lib")9 从键盘抓取输入 也就是抓取用户从键盘输入的字符。 unix平台: Ruby代码 def getchar
system("stty raw -echo")
char = STDIN.getc
system("stty -raw echo")
def getchar
system("stty raw -echo")
# Raw mode, no echo
char = STDIN.getc
system("stty -raw echo")
# Reset terminal mode
endwindows平台: Ruby代码 require 'Win32API'
def getchar
char = Win32API.new("crtdll", "_getch", [], 'L').Call
require 'Win32API'
def getchar
char = Win32API.new("crtdll", "_getch", [], 'L').Call
end10 读取整个文件到内存 读取整个文件到数组,你不需要打开文件,IO.readlines 可以完成这个工作,他自己会open和close. Ruby代码 arr = IO.readlines("myfile")
lines = arr.size
puts "myfile has #{lines} lines in it."
longest = arr.collect {|x| x.length}.max
puts "The longest line in it has #{longest} characters."
arr = IO.readlines("myfile")
lines = arr.size
puts "myfile has #{lines} lines in it."
longest = arr.collect {|x| x.length}.max
puts "The longest line in it has #{longest} characters."也可以用IO.read(它返回一个大的字符串): Ruby代码 str = IO.read("myfile")
bytes = arr.size
puts "myfile has #{bytes} bytes in it."
longest = str.collect {|x| x.length}.max
puts "The longest line in it has #{longest} characters."
str = IO.read("myfile")
bytes = arr.size
puts "myfile has #{bytes} bytes in it."
longest = str.collect {|x| x.length}.max
# strings are enumerable!
puts "The longest line in it has #{longest} characters."
由于File继承了IO,因此File也有这两个方法. 11 逐行迭代一个文件 我们可以使用IO.foreach 方法,或者each方法,如果是前者文件不需要显示打开: Ruby代码
IO.foreach("somefile") do |line|
puts line if line =~ /target/
file = File.new("somefile")
file.each do |line|
puts line if line =~ /target/
# Print all lines containing the word "target"
IO.foreach("somefile") do |line|
puts line if line =~ /target/
# Another way...
file = File.new("somefile")
file.each do |line|
puts line if line =~ /target/
end12逐字节对文件进行遍历 可以使用each_byte方法,如果你想要转换byte到字符的话使用chr方法: Ruby代码 file = File.new("myfile")
e_count = 0
file.each_byte do |byte|
e_count += 1 if byte == ?e
file = File.new("myfile")
e_count = 0
file.each_byte do |byte|
e_count += 1 if byte == ?e
end12 把字符串当文件来用 我们可以使用stringio库: Ruby代码 require 'stringio'
ios = StringIO.new("abcdefghijkl\nABC\n123")
ios.seek(5)
ios.puts("xyz")
puts ios.tell
puts ios.string.dump
c = ios.getc
puts "c = #{c}"
ios.ungetc(?w)
puts ios.string.dump
puts "Ptr = #{ios.tell}"
s1 = ios.gets
s2 = ios.gets
require 'stringio'
ios = StringIO.new("abcdefghijkl\nABC\n123")
ios.seek(5)
ios.puts("xyz")
puts ios.tell
puts ios.string.dump
# "abcdexyzijkl\nABC\n123"
c = ios.getc
puts "c = #{c}"
ios.ungetc(?w)
puts ios.string.dump
# "abcdexyzwjkl\nABC\n123"
puts "Ptr = #{ios.tell}"
s1 = ios.gets
s2 = ios.gets
# "ABC"13读取嵌套在程序中的数据 ruby中程序末尾的__END__ 标记说明,下面的数据是程序内嵌的数据,你可以使用一个IO对象DATA来读取。 Ruby代码
DATA.each_line do |line|
puts line.reverse
A man, a plan, a canal... Panama!
Madam, I'm Adam.
,siht daer nac uoy fI
.drah oot gnikrow neeb ev'uoy
# Print each line backwards...
DATA.each_line do |line|
puts line.reverse
A man, a plan, a canal... Panama!
Madam, I'm Adam.
,siht daer nac uoy fI
.drah oot gnikrow neeb ev'uoy14 读取程序源码 DATA指向__END__ 后面的数据,如果你调用rewind,它将会将文件指针指向程序的开头: Ruby代码 DATA.rewind
浏览 14232
浏览: 93088 次
来自: 上海
不错挺好使.
非常感谢,好用。
ie.Document.getElementById(&quo ...ruby&利用正则分割字符串
字符串是ruby最大的内建类,对于字符串的操作有75个以上的标准方法,下面介绍些使用频繁的方法:
例1:有如下文件
<img HTTP:="" ="" src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />/jazz/j00132.mp3&&&&|
3:45|&&Fats
Waller&&&&&&&&|
Ain't Misbehavin'&
<img HTTP:="" ="" src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
/jazz/j00319.mp3&&&&|
2:58|&&Louis
Armstrong&&&&|Wonderful
<img HTTP:="" ="" src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
/bgrass/bg0732.mp3&&|
4:09|&&Strength&in&Number
| Texas Red
用对字符串的操作实现:
1.每行分割成各个字段;
2.把播放时间从mm:ss转换成秒;
3.删除歌曲演唱者名字中的多余空格
1.分割字段:string#split前面介绍过的方法,对于切割的模式可以用Regexp来匹配,/\s*\|\s*/传递给split,然后分割成字元
class&Song&
&&def initialize(title, name,
duration)&
&&&&@title=title&
&&&&@name=name&
&&&&@duration=duration&
&&attr_reader :title, :name,
:duration&
class&SongList&
initialize&
&& @songs=Array.new&&&
[](index)&
&&&&return&"#{@songs[index].name}
#{@songs[index].title}
#{@songs[index].duration}"&&&
#这句不知道能不能有简单的方法实现?
append(song)&
@songs.push(song)&&
f=File.open('test')&
songs=SongList.new&
f.each&do&|line|&
&&file, length, name, title=
line.chomp.split(/\s*\|\s*/)&
&&song=Song.new(name,
title, length)&
&&songs.append(song)&
puts songs[1]
输出结果:Wonderful world Louis Armstrong 2:58
这不是跟结果体数组一样了么。。
2.有时候在名字保存的时候中间可能加入多余的空格(大于1个)可以用string#squeeze!("
")方法把多个空格挤压成一个#squeeze是"挤压的意思"用!方法是改变字符串的方法。下面代码
#两个class类
f=File.open('test')&
songs=SongList.new&
f.each&do&|line|&
&&file, length, name, title=
line.chomp.split(/\s*\|\s*/)&
&&name.squeeze!("
&&song=Song.new(name,
title, length)&
&&songs.append(song)&
puts songs[1]
Wonderful world Louis Armstrong 2:58
转换时间格式:
在得出length以后可以对length再进行分割,分成 min和secs方法1:
min, secs=length.split(/:/)
duration=min.to_i*60+secs.to_i
min, secs=length.scan(/\d+/)
duration=min.to_i*60+sec.to_i
用string#scan来扫描字符串中满足regexp的字元,这里regexp=/\d+/代表是数字的字元
Wonderful world Louis Armstrong 178
&重写一下第一个步骤的代码:
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />class&Song
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
&&def initialize(format, name,
title, duration)
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
&&&&@format=format
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
&&&&@name=name
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
&&&&@title=title
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
&&&&@duration=duration
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
&&attr_accessor :format, :name,
:title, :duration
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
class&SongList
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
&&def initialize
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
&&&&@songs=Array.new
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
&&def append(song)
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
&&&&@songs.push(song)
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
&&def [](index)
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
&&&&@songs[index]
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
&&def length
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
&&&&@songs.length
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
f=File.open('test')
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
songs=SongList.new
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
f.each&do&|line|
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
&&format, duration, name, title=
line.split(/\s*\|\s*/)
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
&&song=Song.new(format,
name, title, duration)
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
&&songs.append(song)
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
(0..songs.length-1).each&do&&|n|
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
&puts&&"#{songs[n].format}--#{songs[n].name}--#{songs[n].duration}--#{songs[n].title}"
<img src="/blog7style/images/common/sg_trans.gif" real_src ="http://fsjoy./images/editer/InBlock.gif" ALIGN="top" STYLE="padding-top: 0 padding-right: 0 padding-bottom: 0 padding-left: 0 margin-top: 0 margin-right: 0 margin-bottom: 0 margin-left: 0 vertical-align: border-style: border-color: border-style: border-color: border-color: border-width: border-color: border-width: border-color:"
ALT="ruby&利用正则分割字符串"
TITLE="ruby&利用正则分割字符串" />
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。}

我要回帖

更多关于 splitpic使用教程 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信