学习 Ruby 集合我觉得最好的方式是打开 irb --simple-prompt 命令,跟着例子学习。试着借助 Tab 补全加以思考?。

创建数组

和其他动态语言创建相似。

>> a = [1, 2, 3]
=> [1, 2, 3]
>> a.class
=> Array
>> a.length
=> 3
>> a.size
=> 3
>> a[2]
=> 3
>> a << 4
=> [1, 2, 3, 4]
>> a
=> [1, 2, 3, 4]
复制代码

其中类型不必相同,可以同时是 String 、Number 等

?> a = [1, "Grac Kanil", [2, 3]]
=> [1, "Grac Kanil", [2, 3]]
>>
?> a << true
=> [1, "Grac Kanil", [2, 3], true]
>> a << 3.14
=> [1, "Grac Kanil", [2, 3], true, 3.14]
复制代码

一切皆对象,当然数组跑不了这个范畴

?> a = Array.new([1, "Hello", 2])
=> [1, "Hello", 2]
复制代码

数组创建可以传参数,提前申请控件,但是数组是动态的,如果传入数组,会创建包含 nil 的数组。nil 也是一个对象,所以再追加,会在 nil 之后添加。当然可以快速传入默认值,占位数组。

?> a = Array.new(3)
=> [nil, nil, nil]
>>
?> a = Array.new(3, 1)
=> [1, 1, 1]
>>
?>
?> a = Array.new(3)
=> [nil, nil, nil]
>>
?> a << 3
=> [nil, nil, nil, 3]
?> a.size
=> 4
复制代码

切割字符串为数组

  • scan 方法
>> s = "This is a test string."
=> "This is a test string."
>> s.scan(/\w/).join(",")
=> "T,h,i,s,i,s,a,t,e,s,t,s,t,r,i,n,g"
?> s.scan(/\b\w*\b/)
=> ["This", "", "is", "", "a", "", "test", "", "string", ""]
复制代码
  • split 方法
>> s = "This is a test string."
=> "This is a test string."
?> s.split(" ")
=> ["This", "is", "a", "test", "string."]
>> s.split(" ").inspect
=> "[\"This\", \"is\", \"a\", \"test\", \"string.\"]"?> s.split(/\s+/)
=> ["This", "is", "a", "test", "string."]
>> s.split(/\s+/).inspect
=> "[\"This\", \"is\", \"a\", \"test\", \"string.\"]"
复制代码
  • % 来实现的语法捷径
?> est = "est"
=> "est"
>> %W{This is a t#{est} string.}
=> ["This", "is", "a", "test", "string."]
>>
?> %w{This is a t#{est} string.}
=> ["This", "is", "a", "t\#{est}", "string."]
复制代码

数组越界问题

很多语言访问不存在的索引,会抛出数组越界异常,但 ruby 中不会,会返回 nil 哦

?> a = []
=> []
>> a[4]
=> nil
复制代码

cool!? 以上是读,如果写入呢?

?> a
=> []
>> a[3] = "Grac"
=> "Grac"
>> a
=> [nil, nil, nil, "Grac"]
>> a[7] = "Kanil"
=> "Kanil"
>>
?> a
=> [nil, nil, nil, "Grac", nil, nil, nil, "Kanil"]
复制代码

so cool! 在很多语言中,如果你的索引值小于 0 呢,又会怎样?

?> a = [1, 2, 3]
=> [1, 2, 3]
>>
?> a[-1]
=> 3
>>
?> a[-4]
=> nil
复制代码

wonderful!

数组分类

表示数组前两个元素的多种表示方法

?> strings = %w{a b c d e f g}
=> ["a", "b", "c", "d", "e", "f", "g"]
>>
?> strings[0..1]
=> ["a", "b"]
>> strings[0...1]
=> ["a"]
>> strings[0...2]
=> ["a", "b"]
>> strings[0,2]
=> ["a", "b"]
>> strings[-7,2]
=> ["a", "b"]
>> strings[-7..-6]
=> ["a", "b"]
>> strings[-7...2]
=> ["a", "b"]
>> strings[-7..1]
=> ["a", "b"]
>> strings[0..-6]
=> ["a", "b"]
>> strings[0...-5]
=> ["a", "b"]
复制代码

进阶思考

>> (0..1).class
=> Range
?> strings.[](0..1)
=> ["a", "b"]
>> strings.[](Range.new(0,1))
=> ["a", "b"]
复制代码

有趣的 numerouno

➜  ~ gem install numerouno
Fetching: numerouno-0.2.0.gem (100%)
Successfully installed numerouno-0.2.0
Parsing documentation for numerouno-0.2.0
Installing ri documentation for numerouno-0.2.0
Done installing documentation for numerouno after 0 seconds
1 gem installed
➜  ~ irb --simple-prompt?> require 'numerouno'
=> true
>>
?> "sixty one".as_number
=> 61# 为数组添加英文索引
?> class EnglishArray < Array
>>   def [](idx)
>>     if String === idx
>>       self.at(idx.as_number)
>>     end
>>   end
>> end
=> :[]
>>
?> array = EnglishArray.new([1, 2, 3, 4])
=> [1, 2, 3, 4]
>>
?> array["one"]
=> 2
>> array["three"]
=> 4
复制代码

Ruby 数组的万能结构

  • 栈 可以将数组作为。其功能和 Python list 相似,使用 poppush 方法。
?> a = []
=> []
>> a.push(3)
=> [3]
>> a.push("Grac")
=> [3, "Grac"]
>>
?> a.push("Kanil")
=> [3, "Grac", "Kanil"]
>>
?>
?> a.pop
=> "Kanil"
>> a.pop
=> "Grac"
>> a.pop
=> 3
>> a
复制代码
  • 队列 队列,即先进先出
?> a = []
=> []
>> a.push(1)
=> [1]
>> a.push(2)
=> [1, 2]
>> a.push(3)
=> [1, 2, 3]
>> a.shift
=> 1
>> a.shift
=> 2
>> a.shift
=> 3
>> a
=> []
复制代码

还可以使用 unshift 队首加入新元素

?> a = []
=> []
>> a.push(1)
=> [1]
>> a.unshift(2)
=> [2, 1]
>> a.unshift(3)
=> [3, 2, 1]
复制代码

使用 delete 删除任意位置元素

?> a = [1, 2, 3]
=> [1, 2, 3]
>> a.delete(2)
=> 2
>> a
=> [1, 3]
复制代码
  • 集合
?> [1, 2, 3] + [4, 5, 6]
=> [1, 2, 3, 4, 5, 6]?> [1, 2, 3] + [2, 3]
=> [1, 2, 3, 2, 3]>> [1, 2, 3].concat([4, 5, 6])
=> [1, 2, 3, 4, 5, 6]>> ["a", 1, 2, 3, "b"] - ["a", "b"]
=> [1, 2, 3]>> [1, 2, 4] & [1, 2, 3]
=> [1, 2]>> [1, 2, 4] | [1, 2, 3]
=> [1, 2, 4, 3]
复制代码

Ruby 数组常用方法

  • 检查数组是否为空
>> a = []
=> []
>> a.empty?
=> true
复制代码
  • 移动元素
# 转置
?> [1, 2, 4].reverse
=> [4, 2, 1]# 向左移动一
?> [1, 2, 4].rotate
=> [2, 4, 1]# 向右移动一
?> [1, 2, 4].rotate(-1)
=> [4, 1, 2]
复制代码
  • 安全提示
?> a = [1, 2, 3]
=> [1, 2, 3]
>> a.freeze
=> [1, 2, 3]
>> a << 4
RuntimeError: can't modify frozen Arrayfrom (irb):85from /Users/gekang/.rvm/rubies/ruby-2.2.4/bin/irb:11:in `<main>'
复制代码
  • 数组的 join 方法,将元素都链接为字符串
?> a = [1, 2, "Grac", "Kanil"]
=> [1, 2, "Grac", "Kanil"]
>>
?> a.join
=> "12GracKanil"
复制代码

当然,可以传入参数

?> a.join(",")
=> "1,2,Grac,Kanil"
复制代码
  • 删除嵌套
?> [1, [2, 3], [4, ["a", nil]]].flatten
=> [1, 2, 3, 4, "a", nil]
>>
?> [1, [2, 3], [4, ["a", nil]]].flatten(1)
=> [1, 2, 3, 4, ["a", nil]]
复制代码
  • 删除副本
?> [4, 1, 2, 3, 1, 4].uniq
=> [4, 1, 2, 3]
复制代码
  • 检查是否包含某元素
?> a = [1, "a", 2]
=> [1, "a", 2]
>> a.include? "a"
=> true
>> a.include? 1
=> true
>> a.include?(1)
=> true
复制代码
  • 分割数组
?> a = [1, 2, 3, 4, 5, 6]
=> [1, 2, 3, 4, 5, 6]
>> a.first
=> 1
>> a.last
=> 6
>> a.first(3)
=> [1, 2, 3]
>> a.last(4)
=> [3, 4, 5, 6]
复制代码
  • 统计数组中某元素的个数
?> a = [4, 1, 2, 3, 1, 4]
=> [4, 1, 2, 3, 1, 4]
>> a.count 4
=> 2
复制代码
  • 查询数组所有方法
?> Array.methods
=> [:[], :try_convert, :allocate, :new, :superclass, :freeze, :===, :==, :<=>, :<, :<=, :>, :>=, :to_s,
:inspect, :included_modules, :include?, :name, :ancestors, :instance_methods, :public_instance_methods, :protected_instance_methods, :private_instance_methods, :constants, :const_get, :const_set,
:const_defined?, :const_missing, :class_variables, :remove_class_variable, :class_variable_get,
:class_variable_set, :class_variable_defined?, :public_constant, :private_constant, :singleton_class?,
:include, :prepend, :module_exec, :class_exec, :module_eval, :class_eval, :method_defined?,
:public_method_defined?, :private_method_defined?, :protected_method_defined?, :public_class_method, :private_class_method, :autoload, :autoload?, :instance_method, :public_instance_method, :nil?, :=~,
:!~, :eql?, :hash, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint,
:untrust, :untrusted?, :trust, :frozen?, :methods, :singleton_methods, :protected_methods,
:private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap,
:send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval,
:instance_exec, :__send__, :__id__]
复制代码

迭代

迭代可以说是 Ruby 的一大亮点,我觉得 Objective-C 中的迭代就很少使用,比较笨重,更多的会想到写一个 for 循环,然而,Ruby 并不会,优雅的迭代使用起来比较顺手。

each

Ruby 中迭代的核心应该就是 each 方法了

?> a = ["This", "is", "a", "test", "string"]
=> ["This", "is", "a", "test", "string"]
>>
?> a.each {|s| puts s.cap} # Tab 键可以联想
s.capitalize   s.capitalize!  s.captures?> a.each { |s| puts s.capitalize }
This
Is
A
Test
String
=> ["This", "is", "a", "test", "string"]
复制代码

还有 reverse_each 反向迭代

?> a = ["This", "is", "a", "test", "string"]
=> ["This", "is", "a", "test", "string"]
>> a.reverse_each { |s| puts s.up }
s.upcase   s.upcase!  s.update   s.upto
>> a.reverse_each { |s| puts s.upcase }
STRING
TEST
A
IS
THIS
=> ["This", "is", "a", "test", "string"]
复制代码

如果你需要索引,当然也给你准备好了当前处理的索引 each_with_index

>> a = ["This", "is", "a", "test", "string"]
=> ["This", "is", "a", "test", "string"]
>> a.each_with_index { |s, index| puts "#{index} is #{s}" }
0 is This
1 is is
2 is a
3 is test
4 is string
=> ["This", "is", "a", "test", "string"]
复制代码

如果你需要迭代数组中某一部分,可以使用分割数组

?> a = ["This", "is", "a", "test", "string"]
=> ["This", "is", "a", "test", "string"]
>> a[0..2]
=> ["This", "is", "a"]
>> a[0..2].each {|s| puts s}
This
is
a
=> ["This", "is", "a"]
复制代码

也可以使用 Range 来实现

?> a = ["This", "is", "a", "test", "string"]
=> ["This", "is", "a", "test", "string"]
>>
?> (0..2).each {|i| puts a[i]}
This
is
a
=> 0..2
复制代码

map 或 collect

>> a = [1, 2, 3]
=> [1, 2, 3]
>> a.map { |i| i + 1 }
=> [2, 3, 4]
复制代码

如果只是让每一个元素调用方法,不关心每个元素的值时,可以使用快捷写法

>> a = [1, 2, 3]
=> [1, 2, 3]
>> a.map(&:to_f)
=> [1.0, 2.0, 3.0]
复制代码

#map 不改变原来的数组,而是会生成新的数组,如果需要修改原数组,需要使用 #map! 方法

>> a = [1, 2, 3]
=> [1, 2, 3]
>> a.map(&:to_f)
=> [1.0, 2.0, 3.0]
>>
?> a
=> [1, 2, 3]
>> a.map!(&:to_f)
=> [1.0, 2.0, 3.0]
>> a
=> [1.0, 2.0, 3.0]
复制代码

#map#collect 相同,可以互换使用

?> a = [1, 2, 3]
=> [1, 2, 3]
>> a.map(&:to_f)
=> [1.0, 2.0, 3.0]
>> a.collect(&:to_f)
=> [1.0, 2.0, 3.0]
复制代码

Ruby 札记 - Ruby 集合家族之数组(Array)相关推荐

  1. Ruby 学习(六)数组Array

    文章目录 数组 `[]` 基础 索引 常用API 创建 数组的大小 `size` `length` 判断是否为空 `array.empty?` `&` `*` `array * int` `a ...

  2. Java之数组array和集合list、set、map

    2019独角兽企业重金招聘Python工程师标准>>> 世间上本来没有集合,(只有数组参考C语言)但有人想要,所以有了集合   有人想有可以自动扩展的数组,所以有了List   有的 ...

  3. Java ArrayList和Vector、LinkedList与ArrayList、数组(Array)和列表集合(ArrayList)的区别...

    ArrayList和Vector的区别 ArrayList与Vector主要从二方面来说. 一.同步性: Vector是线程安全的,也就是说是同步的,而ArrayList是线程序不安全的,不是同步的. ...

  4. java 数组map_Java之数组array和集合list、set、map

    之前一直分不清楚java中的array,list.同时对set,map,list的用法彻底迷糊,直到看到了这篇文章,讲解的很清楚. 世间上本来没有集合,(只有数组参考C语言)但有人想要,所以有了集合 ...

  5. Java-杂项:Java数组Array和集合List、Set、Map

    ylbtech-Java-杂项:Java数组Array和集合List.Set.Map 1.返回顶部 1. 之前一直分不清楚java中的array,list.同时对set,map,list的用法彻底迷糊 ...

  6. js中数据结构数组Array、映射Map、集合Set、对象、JSON

    全栈工程师开发手册 (作者:栾鹏) js系列教程5-数据结构和算法全解 js中数据结构 js中原生自带的数据结构比较简单,主要有数组Array.映射Map.集合Set.我们可以根据这三个基本数据结构实 ...

  7. Kotlin的数组array和集合list的简单使用以及注意事项

    学习到Kotlin的数组和集合,这里做个记录. 数组Array Kotlin中数组也是个容器,提供了不同的类型有:ByteArray, CharArray, ShortArray, IntArray, ...

  8. 3.1.5 spark体系之分布式计算-scala编程-scala中的集合(数组array、list、set、map、元组)

    目录 1.数组 Array 1.1 创建数组 1.2 concat 方法 1.3 遍历和其他方法 1.4 定义可变数组:ArrayBuffer,第一个是后加,第二个是头加,第三个是追加多个 2.lis ...

  9. SHELL/SSH基础知识(入门篇)-包含 shell 脚本语言的基本用法、 shell 脚本语言的基本用法、流程控制、函数 function、其它脚本相关工具、数组 array(欢迎留言交流)

    目录 1 shell 脚本语言的基本用法 1.1 shell 脚本注释规范 1.1.1 shell 脚本注释规范 1.1.2 执行(5种) 1.1.3 在远程主机运行本地脚本 1.1.4 检查shel ...

最新文章

  1. FILE文件流的中fopen、fread、fseek、fclose的使用
  2. cv python 样例_【CV实战】OpenCV—Hello world代码示例
  3. 【云音乐】从手游APP到云音乐视频标签分类,时隔3个月再次面对多标签任务,终于摸索出了一些小经验。附自定义评估函数代码
  4. PHP面向对象的进阶学习
  5. 【致青春】致成长路上的那些琐事
  6. 移动端实现蓝牙打印机打印_MBrush世界上最小的移动彩色打印机
  7. OpenCV3学习(3)——视频文件的读取与写入
  8. 物联网将如何影响你的钱包?
  9. java值的传递_Java 8可选–基于值的类使用指南
  10. 【LeetCode】【数组】题号:*118,杨辉三角
  11. 彩票站漏洞引发的软件测试漏测思考
  12. 常见的前端视频播放格式
  13. 有道云脑图 mindmap文件必须会员才能导出
  14. C语言之const和volatile究极学习
  15. gomonkey permission denied
  16. Oracle报错:不是单组分组函数解决
  17. 7.24[C语言零基础 知识点总结]
  18. JavaScript知识要点 - Web前端开发必备
  19. python装饰器带参数函数二阶导数公式_【计算机程序的构造和解释】使用函数构建抽象——5. 高阶函数...
  20. 现代化富文本编辑器 Quill Editor

热门文章

  1. 研发游戏引擎那么难,为什么还应该砸钱去干?
  2. 可重入函数和不可重入函数
  3. 啊哈算法-简化版桶排序
  4. 【人机对话】阿里云人工智能ET化身CEO,对话蜻蜓FM猫局长
  5. CSS字体、文本样式
  6. 公有云——阿里云ECS服务器(IaaS)
  7. lichee nano 荔枝派入门——buildroot构建文件系统
  8. Linux .deb文件和.rpm文件的区别
  9. 关于使用微信Web应用开发者----期待我大中华的软件开发能力雄起!
  10. L2-016 愿天下有情人都是失散多年的兄妹 (25 分)