摘要:
#定义一个UTF-8=>GBK的方法def encoding inStr Iconv.iconv("GBK","UTF-8",inStr)end#定义一个GBK=>UTF-8的方法def unencoding inStr Iconv.iconv("UTF-8","GBK",inStr).joinend学习参考:http://www.kuqin.com/rubycndocument/man/addlib/Iconv.html#Iconv.2eiconvIconv 类类方法Iconv.new(to, fro 阅读全文
摘要:
引用链接:http://www.blogjava.net/nkjava/archive/2010/01/03/308088.html1,切片:silce, [ ]-----------------[ ]是silce的别名,所以两者是完全相同的操作1:判定字符串中是否含有字串/子模式string[substring]string[/pattern/]string[/pattern/, position] #position之后的子串中是否含有/pattern/如果存在返回子串/子模式串,否则返回nil“hello world"["hello"]==="he 阅读全文
摘要:
最常见的方式就是使用内置的get 方法,这个方法可以从命令行读取用户的输入,并在默认的情况下把读入的文本赋值给预定义变量$_.但是get方法会保留用户在输入字符串末尾所加的换行符,当用户在输入的字符串结尾输入了句号,并按了回车,得到的是stop\n。ruby中提供了另外一个内置的方法chomp,它会除去$_后面的换行符。 print "please enter anything:" gets chomp puts "The input is #{$_}" 用户的输入可以赋值给一个变量并来替换$_,但是删除末尾的空行就有问题,因为chmop只能对$_操作, 阅读全文
摘要:
1.load( io )Load a document from the current io stream.File.open( 'animals.yaml' ) { |yf| YAML::load( yf ) } #=> ['badger', 'elephant', 'tiger']example:require 'yaml'yml = YAML::load(File.open('t.yml'))p yml Can also load from a string.YAML.load( &q 阅读全文
摘要:
1.sort → new_ary click to toggle sourcesort { |a, b| block } → new_aryReturns a new array created by sorting self.Comparisons for the sort will be done using the operator or using an optional code block.The block must implement a comparison between a and b, and return -1, when a follows b, 0 when a. 阅读全文
摘要:
由[索引, 值, ...] 型的数组变为哈希表ary = [1,"a", 2,"b", 3,"c"]p Hash[*ary]# => {1=>"a", 2=>"b", 3=>"c"}由索引和值配对出现的数组变为哈希表alist = [[1,"a"], [2,"b"], [3,"c"]]p Hash[*alist.flatten]#=> {1=>"a", 2=&g 阅读全文
摘要:
http://rubydoc.info/gems/watir-webdriver/frameshttp://rdoc.info/gems/watir-webdriver/frameshttp://www.ruby-doc.org/core-2.0/Array.html 阅读全文
摘要:
原文链接:http://www.ruby-doc.org/core-1.9.3/Regexp.htmlRegexpA Regexp holds a regular expression, used to match a pattern against strings. Regexps are created using the /.../ and %r{...} literals, and by the Regexp::new constructor.Regular expressions (regexps) are patterns which describe the contents o 阅读全文
摘要:
1.puts /[一-龥]+/.match("this is 中文") =>中文2.str2="123中文"puts /\p{Han}+/u.match(str2)文本编码格式:utf-8文件第一行:#encoding:utf-8require "rubygems"require "iconv"print Iconv.iconv("GBK","UTF-8",/\p{Han}+/u.match("tiantianxin你好angshang天天向上")[ 阅读全文
摘要:
假设一个字符串当中有很多符合规则的信息,下面的例子可以把所有匹配到的结果打印出来:message="afhadhffkdf414j9tr3j43i3433094jwoert223jwew123dfdf"regex=/[a-z](\d{3})[a-z]/ message.scan(regex).each{|m|puts"Test#{m[0]}"} message="afhadhffkdf414j9tr3j43i3433094jwoert223jwew123dfdf"regex=/[a-z](\d{3})[a-z]/ message.sc 阅读全文