Ruby Hash keys()函数

Ruby Hash keys()函数

Hash#keys()是一个Hash类方法,它给出了一个包含哈希中所有键的数组。

语法。Hash.keys()

参数:哈希值

返回:包含散列中所有键的数组

例子 #1 :

# Ruby code for Hash.keys() method
  
# declaring Hash value
a = {a:100, b:200}
  
# declaring Hash value
b = {a:100, c:300, b:200}
  
# declaring Hash value
c = {a:100}
  
  
# keys Value
puts "Hash a keys form : #{a.keys()}\n\n"
  
puts "Hash b keys form : #{b.keys()}\n\n"
  
puts "Hash c keys form : #{c.keys()}\n\n"

输出:

Hash a keys form : [:a, :b]

Hash b keys form : [:a, :c, :b]

Hash c keys form : [:a]

例子 #2 :

# Ruby code for Hash.keys() method
  
# declaring Hash value
a = { "a" => 100, "b" => 200 }
  
# declaring Hash value
b = {"a" => 100}
  
# declaring Hash value
c = {"a" => 100, "c" => 300, "b" => 200}
  
  
# keys Value
puts "Hash a keys form : #{a.keys()}\n\n"
  
puts "Hash b keys form : #{b.keys()}\n\n"
  
puts "Hash c keys form : #{c.keys()}\n\n"

输出:

Hash a keys form : ["a", "b"]

Hash b keys form : ["a"]

Hash c keys form : ["a", "c", "b"]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程