文章目录
Part2 数据类型
几个Ruby提供的类
Ruby支持的数据类型包括基本的Number
、String
、Ranges
、Symbols
,以及true
、false
和nil
这几个特殊值,同时还有两种重要的数据结构——Array
和Hash
。
字符串 — String类
字符串创建
可以用'
和"
来创建字符串,'
和"
无区别
但是如果要使用内插,就必须使用"
来构建,所以最好使用"
常用方法
方法调用方式 | 返回值 | 描述 |
---|---|---|
str.length | Integer | 字符串的长度,空字符串返回 0 |
str.include?(other_str) | True or False | 字符串包含子串,则返回True ,否则返回False |
str.insert(index, other_str) | new_str | 插入,index 是下标,other_str 是插入串,返回插入后新的字符串 |
str.split(pattern=$;, [limit]) | Array | 分割,按照pattern 分割,默认分割符为空格,返回字符串数组 |
str.gsub(pattern, replacement) | new_str | 替换,将字符串按照pattern 匹配的字符更换为replacement ,返回替换后的字符串 |
str.replace(other_str) | other_str | 整体替换,将字符串整体替换成新的字符串 |
str.delete([other_str]+) | new_str | 删除,参数[other_str]+ 可包含多个字符,该方法匹配到str 中的所有字符并删除,返回新的字符串 |
str.strip | new_str | 清除空白字符,清除掉str 中字符串前后的所有空格,换行符,回车符。返回新的字符串 |
str.reverse | reverse_str | 字符串翻转,将字符串顺序翻转,返回翻转后的字符串 |
str.to_i | Integer | 字符串转数字, 如果字符串以数字开头,则转换为开头数字的整型值,如果字符串不以数字开头,则返回 0 |
str.to_sym | Symbol | 字符串转符号 |
str.chomp | new_str | 去掉字符串末尾的一个(0个)\n 或\r |
str.chop | new_str | 去掉字符串末尾的最后一个字符,不管是\n ,\r 还是普通字符 |
str.downcase | new_str | 将字符串转换为全小写 |
str.upcase | new_str | 将字符串转换为全大写 |
pattern
str = str.gsub(/a*[A-Za-z]+/, "hhh") # 注意格式 /.../
数组 — Array类
数组长度动态增长,元素不限制类别
创建数组
-
直接创建
arr = [1, 2, 3]
-
new方法
arr1 = Array.new # 创建了一个空数组 arr2 = Array.new(5) # 创建了一个长度为5的数组 [nil, nil, nil, nil, nil] arr3 = Array.new(5, "new") # 创建并初始化数组 ["new", "new", "new", "new", "new"]
-
其他
arr4 = Array.[](1, 2, 3) # [1, 2, 3] arr5 = Array[1, 2, 3] # [1, 2, 3]
访问数组元素
索引与Python一致 0 ~ n-1 / -n ~ -1
-
直接索引
arr[index]
不存在返回
nil
-
索引(闭)区间
arr[start, end]
如果这个索引区间内不包含任何元素,则返回
nil
-
索引范围
arr[start..end]
效果相当于索引区间
-
at法
arr.at(index)
arr = [0, 1, 2, 3, 4]
arr[100] # nil
arr[1, 4] # [1, 2, 3, 4]
arr[1..4] # [1, 2, 3, 4]
arr[1..-2] # [1, 2, 3]
arr.at(0) # 0 equal to arr[0]
arr.first
:数组第一个元素arr.last
:数组的最后一个元素arr.take(n)
:数组的前n个元素arr.drop(n)
:索引为n到最后的所有元素
常用方法
方法调用方式 | 返回值 | 描述 |
---|---|---|
arr.empty? | true /false | 判段数组是否为空 |
arr.push(element) | new_array | 在数组末尾加入元素element ,返回加入元素后的新数组 |
arr << element | new_array | 同上 |
arr.insert (index, elements) | new_array | 在位置index 插入元素elements ,可以插多个元素 如arr.insert (3, 'a', 'b', 'c') |
arr.delete(element) | element | 删除所有为element 的元素 |
arr.compact | new_array | 删除所有空元素nil ,返回删除空元素后的新数组 |
arr.uniq | new_array | 去重,返回去重后的新数组 |
arr.reverse | new_array | 翻转,返回翻转后的新数组 |
arr.clear | [ ] | 清空,返回空数组 |
arr.count | Integer | 没有参数时,返回数组的大小,带有参数时,返回数组中与参数相同元素的个数 |
arr.includes?(element) | true /false | 判断数组中是否包含元素element |
arr.sort | new_array | 默认按照字典序排序,也可定制排序规则 |
arr.sample | element | 从数组中随机取样,带参数<取样数量>,则可取多个样本 |
arr.flatten | new_array | 将多维数组转换成一维数组 |
arr.join(',') | String | 将数组使用连接符, 连接成一个字符串 |
哈希 — Hash
键值对, 通过一个不存在的键访问哈希,则方法会返回nil
。
创建Hash
-
直接创建
hash1 = {} hash2 = {"Ruby" => 1, "Python" => 2}
-
new方法
hash1 = Hash.new hash2 = Hash.new(<default_value>) # 设置Hash默认值
通常通过一个不存在的键访问哈希,则方法会返回
nil
;但是设置默认值后,访问不存在的键返回该默认值 -
增添键值对
hash[<键>] = <值>
访问Hash
-
索引访问
hash["Ruby"] # => 1
-
查看键名
hash.keys # ["Ruby", "Python"]
-
查看所有值
hash.values # [1, 2]
常用方法
方法调用方式 | 返回值 | 描述 |
---|---|---|
hash == other_hash | true / false | 判断两个哈希是否相等。键相等,键对应值相等返回true |
hash.clear | {} | 清空 |
hash.delete(key) | value | 删除匹配key 的键值对,并返回对应的值value |
hash.has_key?(key) | true / false | 是否包含键key |
hash.has_value?(value) | true / false | 是否包含值value |
hash.to_s | String | 将哈希的内容转换为字符串输出 |
hash.invert | new_hash | 将哈希的键值对颠倒,键变值,值变键,构成新的哈希返回 |
hash.key(value) | key | 给值找键。如果找不到该值,则返回0 |
hash.length | Integer | 返回哈希的大小 |
hash.merge(other_hash) | new_hash | 将hash 和other_hash 合并成一个新的哈希。重复键的项值采用other_hash 。 |
hash.store(key, value) | value | 加入新的键值对 |
hash.to_a | Array | 将哈希转换为二维数组,格式为: [[ key1, value1 ], [ key2, value2 ]] |
符号 — Symbol
符号Symbol的形式::symbol_name
,一般作为名称标签使用,表示方法等对象的名称
Symbol 比 String 处理更高效,二者可以相互转化
一个
symbol
为一个Object
,所以Symbol更加节省内存