如何从Ruby中的散列表中获取第一个键和值对

时间:2022-01-01 20:57:37

I'm trying to get the first key and value key from a hash table in ruby. I don't know the key values of the hash because it is passed to the method. I cant find anywhere online how to find the first key/value as a separate hash table. I think hash[0] will just try to find an element with a name 0 it just returns nil when I run the code.

我正在尝试从ruby中的散列表中获取第一个键和值键。我不知道哈希的键值,因为它被传递给了方法。我无法在网上找到任何地方找到第一个键/值作为一个单独的散列表。我认为哈希[0]会尝试找到一个名字为0的元素它只会在我运行代码时返回nil。

I know I can find the key name and the value and then create a new hash from them but i wonder if there is an easier way to do this so I get a hash right away.

我知道我可以找到键名和值,然后从它们创建一个新的散列,但我想知道是否有更简单的方法来做这个,所以我马上就得到一个哈希。

here is my code:

这是我的代码:

def rps_game_winner(game)

rock_in_hash = game.invert['R']
paper_in_hash = game.invert['P']
scissors_in_hash = game.invert['S']

if(rock_in_hash)
      if(paper_in_hash)
        return paper_in_hash;
      elsif(scissors_in_hash)
        return rock_in_hash
      end
    elsif(paper_in_hash)
      if(rock_in_hash)
        return paper_in_hash
      elsif(scissors_in_hash)
        return scissors_in_hash
      end
    end
        key = game.keys[-1]
        value = game.values[-1]
            winner = {key => value}
    return winner 
    end

game_one = { "Bob" => 'P', "Jim" => 'P' }

puts rps_game_winner(game_one)

This gets me the correct result the problem is I don't understand why it's -1 instead of zero... And i was hoping there was a better way to get the first key/value pair of a hash table instead of creating new hash table with the key and value you retrieved from the previous table.

这让我得到了正确的结果问题是我不明白为什么是-1而不是0。我希望有一种更好的方法来获取哈希表的第一个键/值对,而不是使用从前一个表中检索到的键和值创建新的哈希表。

3 个解决方案

#1


112  

You can just do

你可以做

key, value = hash.first

or if you prefer:

或者如果你喜欢:

key = hash.keys[0]
value = hash.values[0]

Then maybe:

那么:

new_hash = {key => value}

#2


29  

There is a shorter answer that does not require you to use extra variables:

有一个简短的答案,不需要你使用额外的变量:

h = { "a" => 100, "b" => 200 , "c" => 300, "d" => 400, "e" => 500}
Hash[*h.first] #=> {"a" => 100}

Or if you want to retrieve a key/value at a any single position

或者如果您想在任意位置检索键/值

Hash[*h.to_a.at(1)] #=> {"b" => 200}

Or retrieve a key/values from a range of positions:

或从一系列位置检索键/值:

 Hash[h.to_a[1,3]] #=> {"b"=>200, "c"=>300, "d"=>400}

#3


-3  

I think you still need to read ruby basics first,Any way here is the answer and links for basics of ruby.

我认为您仍然需要首先阅读ruby基础,这里是ruby基础的答案和链接。

h = { "a" => 100, "b" => 200 }
h.each {|key, value| puts "#{key} is #{value}" }
produces:
a is 100
b is 200


h.each_key{|key| puts "key is #{key} "
produces:
key is a
key is b

h.each_value{|value| puts "value is #{value} "
produces:
value is 100
value is 200

Here in the above answer we are using each,each_key,each_value to get the values from hash. each will give you each key,value. each_key will give you each key. each_value will give you each value.

在上面的答案中,我们使用each each_key和each_value从散列中获取值。每个会给你每个键,值。每个密钥都会给出每个密钥。each_value将给出每个值。

Here are the links for basics of ruby.

下面是ruby基础的链接。

http://ruby-doc.org/core-1.9.2/Hash.html

http://ruby-doc.org/core-1.9.2/Hash.html

Ruby Basics

Ruby基础知识

#1


112  

You can just do

你可以做

key, value = hash.first

or if you prefer:

或者如果你喜欢:

key = hash.keys[0]
value = hash.values[0]

Then maybe:

那么:

new_hash = {key => value}

#2


29  

There is a shorter answer that does not require you to use extra variables:

有一个简短的答案,不需要你使用额外的变量:

h = { "a" => 100, "b" => 200 , "c" => 300, "d" => 400, "e" => 500}
Hash[*h.first] #=> {"a" => 100}

Or if you want to retrieve a key/value at a any single position

或者如果您想在任意位置检索键/值

Hash[*h.to_a.at(1)] #=> {"b" => 200}

Or retrieve a key/values from a range of positions:

或从一系列位置检索键/值:

 Hash[h.to_a[1,3]] #=> {"b"=>200, "c"=>300, "d"=>400}

#3


-3  

I think you still need to read ruby basics first,Any way here is the answer and links for basics of ruby.

我认为您仍然需要首先阅读ruby基础,这里是ruby基础的答案和链接。

h = { "a" => 100, "b" => 200 }
h.each {|key, value| puts "#{key} is #{value}" }
produces:
a is 100
b is 200


h.each_key{|key| puts "key is #{key} "
produces:
key is a
key is b

h.each_value{|value| puts "value is #{value} "
produces:
value is 100
value is 200

Here in the above answer we are using each,each_key,each_value to get the values from hash. each will give you each key,value. each_key will give you each key. each_value will give you each value.

在上面的答案中,我们使用each each_key和each_value从散列中获取值。每个会给你每个键,值。每个密钥都会给出每个密钥。each_value将给出每个值。

Here are the links for basics of ruby.

下面是ruby基础的链接。

http://ruby-doc.org/core-1.9.2/Hash.html

http://ruby-doc.org/core-1.9.2/Hash.html

Ruby Basics

Ruby基础知识