Skip to content

Instantly share code, notes, and snippets.

@daiando
Created December 8, 2015 01:03
Show Gist options
  • Save daiando/3593d11b64c0cc2945b0 to your computer and use it in GitHub Desktop.
Save daiando/3593d11b64c0cc2945b0 to your computer and use it in GitHub Desktop.
ruby>enumerator>any, all, none, find
def func_any(hash)
# Check and return if any key object within the hash is of the type Integer
hash.any? {|key, value| key.is_a? Integer}
end
def func_all(hash)
# Check and return if all the values within the hash are Integers and are < 10
hash.all? {|key, value| value < 10}
end
def func_none(hash)
# Check and return if none of the values within the hash are nil
hash.none? {|key, value| value.nil?}
end
def func_find(hash)
# Check and return the first object that satisfies the property
# [key, value] pair where the key is an Integer and the value is < 20
# or [key, value] pair where the key is a String and the value is a String starting
# with the character `a`
hash.find {|key, value| key.is_a?(String) && value.to_i < 20 || key.is_a?(String) && value.to_s.start_with?("a") }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment