Hash - key+value

Hello

Is there a method for directly getting [key, value]-pairs? (like .keys)
hash.to_a seems to build the whole array before retuning.

thanks
Opti

Cheers

robert

···

On Tue, Mar 7, 2017 at 7:35 PM, Die Optimisten <inform@die-optimisten.net> wrote:

Is there a method for directly getting [key, value]-pairs? (like .keys)
hash.to_a seems to build the whole array before retuning.

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Each or each_pair yields key value pairs.

···

On 8 Mar. 2017 5:35 am, "Die Optimisten" <inform@die-optimisten.net> wrote:

Hello

Is there a method for directly getting [key, value]-pairs? (like .keys)
hash.to_a seems to build the whole array before retuning.

thanks
Opti

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Hi Robert,
I looked at it already, but I didn't find any, so there is no?
"a method for DIRECTLY getting the pairs (like .keys)"

thx Opti

···

On 2017-03-07 20:29, Robert Klemme wrote:

On Tue, Mar 7, 2017 at 7:35 PM, Die Optimisten > <inform@die-optimisten.net> wrote:

Is there a method for directly getting [key, value]-pairs? (like .keys)
hash.to_a seems to build the whole array before retuning.

Class: Hash (Ruby 2.4.0)

Cheers

robert

So you didn't find `.each` (each_pair) or `.to_a` to be "direct" enough? Since .keys returns and array and you specifically say "like .keys", why isn't Hash#to_a EXACTLY what you're looking for?

-Rob

···

On 2017-Mar-7, at 17:00 , Die Optimisten <inform@die-optimisten.net> wrote:

On 2017-03-07 20:29, Robert Klemme wrote:

On Tue, Mar 7, 2017 at 7:35 PM, Die Optimisten >> <inform@die-optimisten.net> wrote:

Is there a method for directly getting [key, value]-pairs? (like .keys)
hash.to_a seems to build the whole array before retuning.

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

Cheers

robert

Hi Robert,
I looked at it already, but I didn't find any, so there is no?
"a method for DIRECTLY getting the pairs (like .keys)"

thx Opti

Hi
as mentioned earlier I expect that .to_a builds the full array (consuming a lot of ram), so .each {...} seems to be the best. (I have to write my own .keys_values method)

thanks
Opti

···

On 2017-03-07 23:05, Rob Biedenharn wrote:

On 2017-Mar-7, at 17:00 , Die Optimisten <inform@die-optimisten.net> wrote:

On 2017-03-07 20:29, Robert Klemme wrote:

On Tue, Mar 7, 2017 at 7:35 PM, Die Optimisten >>> <inform@die-optimisten.net> wrote:

Is there a method for directly getting [key, value]-pairs? (like .keys)
hash.to_a seems to build the whole array before retuning.

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

Cheers

robert

Hi Robert,
I looked at it already, but I didn't find any, so there is no?
"a method for DIRECTLY getting the pairs (like .keys)"

thx Opti

So you didn't find `.each` (each_pair) or `.to_a` to be "direct" enough? Since .keys returns and array and you specifically say "like .keys", why isn't Hash#to_a EXACTLY what you're looking for?

-Rob

​I'm with robert and Rob...

···

On 8 March 2017 at 08:47, Die Optimisten <inform@die-optimisten.net> wrote:

On 2017-03-07 23:05, Rob Biedenharn wrote:

On 2017-Mar-7, at 17:00 , Die Optimisten <inform@die-optimisten.net> >>> wrote:

On 2017-03-07 20:29, Robert Klemme wrote:

On Tue, Mar 7, 2017 at 7:35 PM, Die Optimisten >>>> <inform@die-optimisten.net> wrote:

Is there a method for directly getting [key, value]-pairs? (like .keys)

hash.to_a seems to build the whole array before retuning.

Class: Hash (Ruby 2.4.0)

Cheers

robert

Hi Robert,

I looked at it already, but I didn't find any, so there is no?
"a method for DIRECTLY getting the pairs (like .keys)"

thx Opti

So you didn't find `.each` (each_pair) or `.to_a` to be "direct" enough?
Since .keys returns and array and you specifically say "like .keys", why
isn't Hash#to_a EXACTLY what you're looking for?

-Rob

Hi
as mentioned earlier I expect that .to_a builds the full array (consuming
a lot of ram), so .each {...} seems to be the best. (I have to write my own
.keys_values method)

thanks
Opti

~~~
hsh = {:a=>'aaa', :b=>'bbb'}
hsh.keys #=> [:a, :b]
hsh.values #=> ['aaa', 'bbb']
hsh.to_a #=> [[:a,'aaa'], [:b,'bbb']]
~~~

#to_a is the [key,value] equivalent of #keys and #values

~~~
hsh.each_key {|k| puts k }
hsh.each_value {|v| puts v }
hsh.each_pair {|k,v| puts "#{k}: #{v}" }
~~~

#each_pair is the [key,value] equivalent of #each_key and #each_value

I'd be really interested to see your #keys_values method to see what you're
trying to do, that you can't already.

Cheers
--
  Matthew Kerwin
  http://matthew.kerwin.net.au/

as mentioned earlier I expect that .to_a builds the full array (consuming a lot of ram), so .each {...} seems to be the best. (I have to write my own .keys_values method)

I suspect that this is just me being stupid, but given that you don't want an array of all the keys and values, but just one key/value pair --- does that not mean that you must already know which of the many values of the hash you want? So you must already have the key?

In which case, since you already have the key, you only need the value, which is obtained of course with `hash[key]'

If you only want one key/value pair from a hash but you *don't* have the key --- you probably don't want to use a hash?

Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer&gt;

I'd be really interested to see your #keys_valuesmethod to see what you're trying to do, that you can't already.

Hi Matthew
nothing special, just a method like .keys, and using each_pair{...}, avoids the building of an array (like .to_a)
- Could be an already defined method, so I have to define this one-liner - just for DRY.
=> hash.keysValues # is much nicer than everytime {||...}

Opti

​That doesn't help me understand.

These two statements are at odds with each other: "...like .keys..." and
"...avoids the building of an array"

The whole purpose of #keys is to build an array. I don't understand how it
can be like #keys and *not* build an array.

Could you show either an implementation of your method, or a demonstrative
example that illustrates its inputs/outputs/side-effects and how it should
be used?

Cheers

···

On 10 March 2017 at 08:35, Die Optimisten <inform@die-optimisten.net> wrote:

I'd be really interested to see your #keys_values method to see what
you're trying to do, that you can't already.

Hi Matthew
nothing special, just a method like .keys, and using each_pair{...},
avoids the building of an array (like .to_a)
- Could be an already defined method, so I have to define this one-liner -
just for DRY.
=> hash.keysValues # is much nicer than everytime {||...}

Opti

--
  Matthew Kerwin
  http://matthew.kerwin.net.au/