一、数字
  1. Ruby支持整数和浮点数,整数可以是任意长度
  2. 一定范围内的整数以二进制存放,它们属于fixnum类型,当超出这个范围时则自动转换为bignum类型
  3. 表达方式:符号+一串字符,数字串中的下划线会被忽略,(前缀包括:0表示八进制, 0x表示十六进制, 0b表示二进制)123_456_789_123_345_789 # Bignum
    0xaabb # 十六进制
  4. 也可以通过在前面加上问号来得到ASCII码字符对应的整数值和转义序列的值
    ?a # 普通字符
    ?\n # 换行符 (0x0a)
    ?\C-a # CTRL+a (0x01)
    ?\M-a # ALT+a
    ?\M-\C-a # CTRL+ALT+a
    ?\C-? # 删除键
  5. 一个带小数点的数字字面值被转换成Float对象
  6. 所有的数字都是对象,不存在相应的函数而是方法
    exp:
    数字的绝对值是aNumber.abs而不是abs(aNumber)
  7. 整数有用的迭代器
    3.times { print "X " } => X X X 1.upto(5) { |i| print i, " " } =>1 2 3 4 5 99.downto(95) { |i| print i, " " }=>99 98 97 96 95 50.step(80, 5) { |i| print i, " " }=>50 55 60 65 70 75 80
二、字符串
  1. Ruby的字符串是8位字节的简单序列,字符串是String类的对象
  2. 注意转换机制(注意单引号与双引号的区别),如:
    单引号中两个相连的反斜线被替换成一个反斜线,,一个反斜线后跟一个单引号被替换成一个单引号
    'escape using "\\"' >> 转义为"\" 'That\'s right' >> That's right
  3. 双引号支持多义的转义
    "\n"
    #{expr}序列来替代任何的Ruby表达式的值 ,(全局变量、类变量或者实例变量,那么可以省略大括号)
    "Seconds/day: #{24*60*60}" >> Seconds/day: 86400 "#{'Ho! '*3}Merry Christmas" >> Ho! Ho! Ho! Merry Christmas "This is line #$." >> This is line 3
  4. here document来创建一个字符串,end_of_string 为结束符号
    aString = <<END_OF_STRING The body of the string is the input lines up to one ending with the same text that followed the '<<' END_OF_STRING
  5. %q和%Q分别把字符串分隔成单引号和双引号字符串(即%q与%Q后面的符号具有',"的功能)
    %q/general single-quoted string/ >> general single-quoted string
  6. String 常用功能
    String#split:把行分解成字段
    String#chomp:去掉换行符
    String#squeeze:剪除被重复输入的字符
    String#scan:以指定想让块匹配的模式
    exp:
    /jazz/j00132.mp3 | 3:45 | Fats Waller | Ain't Misbehavin'
    /jazz/j00319.mp3 | 2:58 | Louis Armstrong | Wonderful World
    #文件格式如上,要进行分解
    songs = SongList.new
    songFile.each do |line|
    file, length, name, title = line.chomp.split(/\s*\|\s*/)#先chomp,后再分解,/\s*表示任字符
    name.squeeze!(" ")#替换空格
    mins, secs = length.scan(/\d+/)#这里用scan匹配模式
    songs.append Song.new(title, name, mins.to_i*60+secs.to_i)
    end
三、区间
  1. 区间存在于任何地方,如:1到12月。ruby用区间实现了3个不同的特性:序列,条件,间隔。
  2. "..":两个点号创建一个闭区间,"...":而三个点号创建一个右开区间(即右边界不取值)
    exp:0..anArray.length-1 等同于 0...anArray.length
  3. to_a 把区间转换成列表
    exp: ('bar'..'bat').to_a >> ["bar", "bas", "bat"]
  4. 区间的共它的用法
    digits = 0..9
    digits.include?(5) >> true
    digits.min >> 0
    digits.max >> 9
    digits.reject {|i| i < 5 } >> [5, 6, 7, 8, 9]
    digits.each do |digit|
    dial(digit)
    end
  5. ruby能把基于自己定义的对象的区间,要求:这个对象必须能够响应succ方法来返回序列中的下一个对象,并且这个对象必须能够使用<=>运算符来被比较,即常规的比较运算符,
  6. 间隔测试
    puts (1..10).include?(3.14)=> ture
    puts (1..10) === 3.14 => ture
四、正则表达式
  1. 正则表达式是Regexp类型的对象,可以使用构造器显式地创建一个正则表达式,也可以使用字面值形式/pattern/和%r\pattern\来创建
  2. 用Regxp#match(aString)的形式或者匹配运算符=~(正匹配)和!~(负匹配)来匹配字符串了。匹配运算符在String和Regexp中都有定义,如果两个操作数都是字符串,则右边的那个要被转换成正则表达式
    exp:
    a = "Fats Waller"
    a =~ /a/ >> 1
    a =~ /z/ >> nil
    a =~ "ll" >> 7
  3. 上面返回的是匹配字符的位置,其它
    $&接受被模式匹配到的字符串部分
    $`接受匹配之前的字符串部分
    $'接受之后的字符串。
    exp:下面的方法后继都会用到

    def showRE(a,re)
    if a =~ re
    "#{$`}<<#{$&}>>#{$'}" #返回前、中、后
    else
    "no match"
    end
    end
  4. 模式,任何一个表达式都包含一个模式,它用来把正则表达式和字任串匹配
    模式中除了., |, (, ), [, {, +, \, ^, $, *,和?以外的字任都匹配它自己
    如果要匹配这些特殊的字符就需要加上反斜线做前缀,分析上面例字
    /\s*\|\s*/,在\s与|之前都加了/做前缀。
    showRE('kangaroo', /angar/) >> k<<angar>>oo
    showRE('!@%&-_=+', /%&/) >> !@<<%&>>-_=+
    showRE('yes | no', /\|/) >> yes <<|>> no
  5. \后跟一个字母或数字表示一个特定的结构如\s表示字符等。
  6. 锚点 一个正则表达式总是返回找到模式的第一个匹配,如何改变?
    模式^和$分别用来匹配行首和行尾
    序列\A匹配字符串开始的位置,\z和\Z匹配字符串结尾的位置
    \b和\B分别匹配字边界和非字边界
    showRE("this is\nthe time", /^the/) >> this is\n<<the>> time
    showRE("this is\nthe time", /is$/) >> this <<is>>\nthe time
    showRE("this is\nthe time", /\Athis/) >> <<this>> is\nthe time

五、字符类
  1. 这里的字符类不是面向对象的类,只表示这些这字符属于一个特殊的种类
  2. 字符类是用方括号扩起来的字符的集合:[characters]匹配方括号中的所有单字符。[aeiou]匹配元音,[,.:'!?]匹配标点符号等等
    showRE('It costs $12.', /[aeiou]/) >> It c<<o>>sts $12.
  3. 在方括号中的序列c1-c2表示在c1-c2之间也包括c1和c2的所有字符
    a = 'Gamma [Design Patterns-page 123]'
    showRE(a, /[]]/) >> Gamma [Design Patterns-page 123<<]>>
    showRE(a, /[B-F]/) >> Gamma [<<D>>esign Patterns-page 123]
    showRE(a, /[-]/) >> Gamma [Design Patterns<<->>page 123]
    showRE(a, /[0-9]/) >> Gamma [Design Patterns-page <<1>>23]
  4. 紧跟在开括号([)后的是字符^,这表示这个字符类的否定:[^a-z]匹配任何不是小写字母的字符。
  5. 字符类缩写
    序列 形如 [ ... ] 含义
    \d [0-9] Digit character
    \D [^0-9] Nondigit
    \s [\s\t\r\n\f] Whitespace character 匹配一个单空白符
    \S [^\s\t\r\n\f] Nonwhitespace character
    \w [A-Za-z0-9_] Word character
    \W [^A-Za-z0-9_] Nonword character
  6. 重复
    r * 匹配0个或多个r的出现
    r + 匹配一个或多个r的出现
    r ? 匹配0个或1个r的出现
    r {m,n} 匹配最少m最多n个r的出现
    r {m,} 匹配最少m个r的出现

    重复结构有高优先权:即它们仅和模式中的直接正则表达式前驱捆绑
    /ab+/匹配一个"a"后跟一个活着多个"b",而不是"ab"的序列
    /a*/会匹配任何字符串:0个或者多个"a"的任意字符串。 
    exp:
    a = "The moon is made of cheese"
    showRE(a, /\w+/) >> <<The>> moon is made of cheese
    showRE(a, /\s.*\s/) >> The<< moon is made of >>cheese
    showRE(a, /\s.*?\s/) >> The<< moon >>is made of cheese
    showRE(a, /[aeiou]{2,99}/) >> The m<<oo>>n is made of cheese
    showRE(a, /mo?o/) >> The <<moo>>n is made of cheese
  7. 替换
    "|"既匹配它前面的正则表达式或者匹配后面的
    a = "red ball blue sky"
    showRE(a, /d|e/) >> r<<e>>d ball blue sky
    showRE(a, /al|lu/) >> red b<<al>>l blue sky
    showRE(a, /red ball|angry sky/) >> <<red ball>> blue sky
  8. 分组
    圆括号把正则表达式分组,组中的内容被当作一个单独的正则表达式

    showRE('banana', /(an)+/) >> b<<anan>>a
    # 匹配重复的字母
    showRE('He said "Hello"', /(\w)\1/) >> He said "He<<ll>>o"
    # 匹配重复的子字符串
    showRE('Mississippi', /(\w+)\1/) >> M<<ississ>>ippi
  9. 基于模式的替换
    你是否想过,大小写替换。
    方法String#sub和String#gsub都在字符串中搜索匹配第一个参数的部分,然后用第二个参数来替换它们。String#sub只替换一次,而String#gsub替换所有找到的匹配。都返回一个包含了替换的新的字符串的拷贝。进化版本是String#sub!和 String#gsub!
    a = "the quick brown fox"
    a.sub(/[aeiou]/, '*') >> "th* quick brown fox"
    a.gsub(/[aeiou]/, '*') >> "th* q**ck br*wn f*x"
    a.sub(/\s\S+/, '') >> "the brown fox"
    a.gsub(/\s\S+/, '') >> "the"
    第二个参数可以是代码块
    a = "the quick brown fox"
    a.sub (/^./) { $&.upcase } >> "The quick brown fox"
    a.gsub(/[aeiou]/) { $&.upcase } >> "thE qUIck brOwn fOx"
  10. 反斜线序列用在替换中
    \& 后面的匹配
    \+ 后面的匹配组
    \` 匹配前面的字符串
    \' 匹配后面的字符串
    \\ 反斜线的字面值
  11. 面向对象的正则表达式
    正则表达式的字面值创建Regexp类
    re = /cat/
    re.type >> Regexp
    方法Regexp#match从字符串中匹配一个正则表达式,如果不成功,方法返回nil,如果成功,返回MatchData类的一个实例
    exp:
    e = /(\d+):(\d+)/ # match a time hh:mm
    md = re.match("Time: 12:34am")
    md.type >> MatchData
    md[0] # == $& >> "12:34"
    md[1] # == $1 >> "12"
    md[2] # == $2 >> "34"
    md.pre_match # == $` >> "Time: "
    md.post_match # == $' >> "am"

以上就是【ruby 标准类型总结】的全部内容了,欢迎留言评论进行交流!

赞(0) 踩(0)

与本文相关的软件

发表我的评论

最新评论

  1. 暂无评论