0

Say I have a hash like:

h = { '0' => 'foo', 'bar' => 'baz' => '2' => 'yada'  }

How would I go about determining if this hash has any numeric keys in it and pulling out the values of those keys?

4 Answers 4

2

try this:

h.select {|key| [*0..9].map(&:to_s).include? key }

remember that I didn't pulled out the value for you, it just returns a selection of your hash with the desired criteria. Pulling the values out this hash like you're used to.

Sign up to request clarification or add additional context in comments.

Comments

2

Another solution:

h.select {|k,v| k.to_i.to_s == k}.values

This returns the values for keys that are integers (positive or negative).

Comments

1

If "numeric" means integer then:

a = h.each_with_object([]) { |(k, v), a| a << v if(k.to_i.to_s == k) }

If "numeric" also includes floating point values then:

h.each_with_object([]) { |(k, v), a| a << v if(k =~ /\A[+-]?\d+(\.\d+)?\z/) }

For example:

>> h = { '0' => 'foo', 'bar' => 'baz', '2' => 'yada', '-3.1415927' => 'pancakes' }
=> {"0"=>"foo", "bar"=>"baz", "2"=>"yada", "-3.1415927"=>"pancakes"}
>> h.each_with_object([]) { |(k, v), a| a << v if(k =~ /\A[+-]?\d+(\.\d+)?\z/) }
=> ["foo", "yada", "pancakes"]

You might want to adjust the regex test to allow leading and trailing whitespace (or not).

Comments

1

Or for a possibly slightly more readable but longer solution, try:

    class String
      def is_i?
        # Returns false if the character is not an integer
        each_char {|c| return false unless /[\d.-]/ =~ c}
        # If we got here, it must be an integer
        true
      end
    end
    h = {"42"=>"mary", "foo"=>"had a", "0"=>"little", "-3"=>"integer"}
    result = []
    h.each {|k, v| result << v if k.is_i?} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.