📜  红宝石 |数组 rindex()函数

📅  最后修改于: 2022-05-13 01:54:19.732000             🧑  作者: Mango

红宝石 |数组 rindex()函数

Array#rindex() : rindex()是一个 Array 类方法,它返回数组中最后一个对象的索引。

示例 #1:

# Ruby code for rindex() method
  
# declaring array
a = [18, 22, 33, nil, 5, 6]
  
# declaring array
b = [1, 4, 1, 1, 88, 9]
  
# declaring array
c = [18, 22, 50, 6]
  
# rindex method example
puts "rindex() method form : #{a.rindex(22)}\n\n"
  
puts "rindex() method form : #{b.rindex(1)}\n\n"
  
puts "rindex() method form : #{c.rindex(18)}\n\n"

输出 :

rindex() method form : 1

rindex() method form : 3

rindex() method form : 0

示例 #2:

# Ruby code for rindex() method
  
# declaring array
a = ["abc", "nil", "dog"]
  
# declaring array
c = ["cat", nil]
  
# declaring array
b = ["cow", nil, "dog"]
  
  
# rindex method example
puts "rindex() method form : #{a.rindex("abc")}\n\n"
  
puts "rindex() method form : #{b.rindex("cat")}\n\n"
  
puts "rindex() method form : #{c.rindex("cat")}\n\n"

输出 :

rindex() method form : 0

rindex() method form : 

rindex() method form : 0