ruby数组操作

简介: irb(main):005:0> [1,1,3,5] & [1,2,3]=> [1, 3]irb(main):006:0> [1,2,3] * 3=> [1, 2, 3, 1, 2, 3, 1, 2, 3]irb(main):007:0> [1,2,3 ] * "--"=> "1--2--3"irb...

irb(main):005:0> [1,1,3,5] & [1,2,3]
=> [1, 3]
irb(main):006:0> [1,2,3] * 3
=> [1, 2, 3, 1, 2, 3, 1, 2, 3]
irb(main):007:0> [1,2,3 ] * "--"
=> "1--2--3"
irb(main):008:0> [1,1,2,2,3,3,4,5]-[1,2,4]
=> [3, 3, 5]
irb(main):009:0> [1,2] <<'c'<<'d'<<[3,4]
=> [1, 2, "c", "d", [3, 4]]
irb(main):010:0> ["a","a","c"] <=> ["a","b","c"]
=> -1
irb(main):011:0> [1,2,3,4,5,6] <=> [1,2]
=> 1
irb(main):012:0> a=["a","b","b","b","c"]
=> ["a", "b", "b", "b", "c"]
irb(main):013:0> a.delete("b")
=> "b"
irb(main):014:0> a.delete("z")
=> nil
irb(main):015:0> a
=> ["a", "c"]

irb(main):016:0> a=["a","b","c"]
=> ["a", "b", "c"]
irb(main):017:0> a.clear
=> []
irb(main):018:0> ["a",nil,"b",nil].compact
=> ["a", "b"]
irb(main):019:0> ["a","b"].compact!
=> nil
irb(main):020:0> ["a","b"].concat(["c","d"])
=> ["a", "b", "c", "d"]



相关文章
|
7月前
|
Ruby
|
7月前
|
Ruby 索引
|
7月前
|
存储 Java Ruby
|
索引 Ruby
【Ruby on Rails全栈课程】2.6 ruby的数据结构--数组(Array)
数组是一个集合,但是不仅仅是数字的集合,可以是任何对象(String、 Integer、 Fixnum、 Hash、 Symbol 等对象)的集合。数组的索引是从0开始的有序整数,可以通过正数索引或者负数索引来寻找数组中的值,数组中的值是有顺序的。
96 0
ruby数组与散列表
%q中定义的字符串,不加分号  如'hello'与%q(hello)  表示同一字符串 %w表示数组,不对变量进行替换;%W表示数组,对变量进行替换 散列表用=>序列进行分隔 irb(main):001:0> %w(fred wilma barney betty great\ gazoo)=>...
571 0
|
7月前
|
Ruby
|
7月前
|
JSON 数据格式 Ruby