Ruby Enumerables: 'any', 'all', 'none', and 'find'

Sort by

recency

|

47 Discussions

|

  • + 0 comments
    def func_any(hash)
      # Check and return true if any key object within the hash is of the type Integer
      # If not found, return false.
      hash.keys.any? { |key| key.is_a?(Integer) }
    end
    
    def func_all(hash)
      # Check and return true if all the values within the hash are Integers and are < 10
      # If not all values satisfy this, return false.
      hash.all? { |key, value| value.is_a?(Integer) && value < 10 }
    end
    
    def func_none(hash)
      # Check and return true if none of the values within the hash are nil
      # If any value contains nil, return false.
      hash.none? { |key, value| value.nil? }
    end
    
    def func_find(hash)
      # Check and return the first object that satisfies either of the following properties:
      #   1. There is a [key, value] pair where the key and value are both Integers and the value is < 20 
      #   2. There is a [key, value] pair where the key and value are both Strings and the value starts with `a`.
      hash.find do |key, value| 
        (key.is_a?(Integer) && value.is_a?(Integer) && value < 20) ||
        (key.is_a?(String) && value.is_a?(String) && value.start_with?('a'))
      end
    end
    
  • + 0 comments
    def func_any(hash)
        return hash.any? {|key, value| key.is_a? Integer}
    end
    
    def func_all(hash)
        return hash.all? {|key, value| value < 10}
    end
    
    def func_none(hash)
        return hash.none? {|key, value| value == nil}
    end
    
    def func_find(hash)
        return hash.find {|key, value| (key.is_a?(Integer) && value.is_a?(Integer) && value < 10) || (key.is_a?(String) && value.is_a?(String) && value.start_with?('a'))}
    end
    
  • + 0 comments
    def func_any(hash)
      # Check and return true if any key object within the hash is of the type Integer
      # If not found, return false.
      hash.any? {|key,value| key.is_a? Integer}
    end
    
    def func_all(hash)
      # Check and return true if all the values within the hash are Integers and are < 10
      # If not all values satisfy this, return false.
      hash.all? {|key, value| (value.is_a? Integer and value < 10)}
    end
    
    def func_none(hash)
      # Check and return true if none of the values within the hash are nil
      # If any value contains nil, return false.
      hash.none? {|key, value| value.nil? }
    end
    
    def func_find(hash)
      # Check and return the first object that satisfies either of the following properties:
      #   1. There is a [key, value] pair where the key and value are both Integers and the value is < 20 
      #   2. There is a [key, value] pair where the key and value are both Strings and the value starts with `a`.
      hash.find {|key, value| ((key.is_a? Integer and value.is_a? Integer and value < 20) or (key.is_a? String and value.is_a? String and value[0] == 'a'))}
    end
    
  • + 0 comments

    I know my code is working, but I get this message:

    There is no input for this challenge. Your code is tested by a hidden checker.

    what should I do to fix this?

  • + 1 comment

    Check this one

    def func_any(hash) hash.any? {|key, value| key.is_a?(Integer)} end

    def func_all(hash) hash.all? {|key, value| value.is_a?(Integer) && value < 10} end

    def func_none(hash) hash.none? {|key, value| value.nil?} end

    def func_find(hash) hash.find do |key, value| (key.is_a?(Integer) && value.is_a?(Integer) && value < 20) || (key.is_a?(String) && value.is_a?(String) && value.start_with?('a')) end end