删除 Ruby 中的数组元素
在本文中,我们将学习如何在 Ruby 中从数组中删除元素。
方法#1:使用索引
Ruby
# Ruby program to remove elements
# in array
# creating string using []
str = ["GFG", "G4G", "Sudo", "Geeks"]
str.delete_at(0)
print str
Ruby
# Ruby program to remove elements
# in array
# creating string using []
str = ["GFG", "G4G", "Sudo", "Geeks"]
str.delete("Sudo")
print str
Ruby
# Ruby program to remove elements
# in array
# creating string using []
str = ["GFG", "G4G", "Sudo", "Geeks"]
str.pop
print str
输出:
["G4G", "Sudo", "Geeks"
方法 #2:使用 delete() 方法 –
红宝石
# Ruby program to remove elements
# in array
# creating string using []
str = ["GFG", "G4G", "Sudo", "Geeks"]
str.delete("Sudo")
print str
输出:
["GFG", "G4G", "Geeks"
方法 #3:使用 pop() 方法 –
红宝石
# Ruby program to remove elements
# in array
# creating string using []
str = ["GFG", "G4G", "Sudo", "Geeks"]
str.pop
print str
输出:
["GFG", "G4G", "Sudo"]