This would be much easier to read with a matcher. Something like this might work:

    Rspec::Matcher.define :have_handedness do |hand|
      match do |layout|
        handed_keys = layout.keys.select { |key| key.hand == hand }
        rows = handed_keys.group_by { |key| key.row_number }.values
    
        rows.all? { |row| row.length == 5 }
      end
    end
    
    it 'has five left-handed keys' do
      expect(layout).to have_handedness(:left)
    end