Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directly get key or value from a single element hash in Ruby [closed]

Tags:

ruby

hash

If I know there is only one key/value pair in a hash, is there a direct way to retrieve either the key or the value directly without having to get all the keys or values?

here is a simple example:

hsh1 = {a: 1} 
hsh2 = {a: 2}
hsh1.keys # => [:a]
hsh2.values # => [2]
hsh1.values + hsh2.values # => [1,2]

Is there a way to get this instead?

1 + 2 # => 3
like image 419
user1297102 Avatar asked Dec 04 '22 10:12

user1297102


1 Answers

hsh1 = {a: 1}
hsh2 = {b: 2}

hsh1.values.first + hsh2.values.first # => 3
like image 134
Sergio Tulentsev Avatar answered Dec 29 '22 00:12

Sergio Tulentsev