We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Ruby hooks failing with rbenv installed
Pre-commit has been failing for the past few weeks.

https://gist.github.com/ThatGerber/d6533155848076b25e5e0d5cb02e20eb

Seems to be an issue with the ruby (rbenv) environment.

Tried running `pre-commit clean && pre-commit` but it returns the same issue. Setting `rbenv global 2.2.4` and `rbenv shell 2.2.4` does help either.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pre_commit/languages/ruby.py`
Content:
```
1 from __future__ import unicode_literals
2 
3 import contextlib
4 import io
5 import os.path
6 import shutil
7 
8 from pre_commit.envcontext import envcontext
9 from pre_commit.envcontext import Var
10 from pre_commit.languages import helpers
11 from pre_commit.util import CalledProcessError
12 from pre_commit.util import clean_path_on_failure
13 from pre_commit.util import resource_filename
14 from pre_commit.util import tarfile_open
15 from pre_commit.xargs import xargs
16 
17 
18 ENVIRONMENT_DIR = 'rbenv'
19 
20 
21 def get_env_patch(venv, language_version):
22     return (
23         ('GEM_HOME', os.path.join(venv, 'gems')),
24         ('RBENV_ROOT', venv),
25         ('RBENV_VERSION', language_version),
26         ('PATH', (
27             os.path.join(venv, 'gems', 'bin'), os.pathsep,
28             os.path.join(venv, 'shims'), os.pathsep,
29             os.path.join(venv, 'bin'), os.pathsep, Var('PATH'),
30         )),
31     )
32 
33 
34 @contextlib.contextmanager
35 def in_env(repo_cmd_runner, language_version):
36     envdir = os.path.join(
37         repo_cmd_runner.prefix_dir,
38         helpers.environment_dir(ENVIRONMENT_DIR, language_version),
39     )
40     with envcontext(get_env_patch(envdir, language_version)):
41         yield
42 
43 
44 def _install_rbenv(repo_cmd_runner, version='default'):
45     directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
46 
47     with tarfile_open(resource_filename('rbenv.tar.gz')) as tf:
48         tf.extractall(repo_cmd_runner.path('.'))
49     shutil.move(
50         repo_cmd_runner.path('rbenv'), repo_cmd_runner.path(directory),
51     )
52 
53     # Only install ruby-build if the version is specified
54     if version != 'default':
55         # ruby-download
56         with tarfile_open(resource_filename('ruby-download.tar.gz')) as tf:
57             tf.extractall(repo_cmd_runner.path(directory, 'plugins'))
58 
59         # ruby-build
60         with tarfile_open(resource_filename('ruby-build.tar.gz')) as tf:
61             tf.extractall(repo_cmd_runner.path(directory, 'plugins'))
62 
63     activate_path = repo_cmd_runner.path(directory, 'bin', 'activate')
64     with io.open(activate_path, 'w') as activate_file:
65         # This is similar to how you would install rbenv to your home directory
66         # However we do a couple things to make the executables exposed and
67         # configure it to work in our directory.
68         # We also modify the PS1 variable for manual debugging sake.
69         activate_file.write(
70             '#!/usr/bin/env bash\n'
71             "export RBENV_ROOT='{0}'\n"
72             'export PATH="$RBENV_ROOT/bin:$PATH"\n'
73             'eval "$(rbenv init -)"\n'
74             'export PS1="(rbenv)$PS1"\n'
75             # This lets us install gems in an isolated and repeatable
76             # directory
77             "export GEM_HOME='{0}/gems'\n"
78             'export PATH="$GEM_HOME/bin:$PATH"\n'
79             '\n'.format(repo_cmd_runner.path(directory))
80         )
81 
82         # If we aren't using the system ruby, add a version here
83         if version != 'default':
84             activate_file.write('export RBENV_VERSION="{0}"\n'.format(version))
85 
86 
87 def _install_ruby(runner, version):
88     try:
89         helpers.run_setup_cmd(runner, ('rbenv', 'download', version))
90     except CalledProcessError:  # pragma: no cover (usually find with download)
91         # Failed to download from mirror for some reason, build it instead
92         helpers.run_setup_cmd(runner, ('rbenv', 'install', version))
93 
94 
95 def install_environment(
96         repo_cmd_runner,
97         version='default',
98         additional_dependencies=(),
99 ):
100     additional_dependencies = tuple(additional_dependencies)
101     directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
102     with clean_path_on_failure(repo_cmd_runner.path(directory)):
103         # TODO: this currently will fail if there's no version specified and
104         # there's no system ruby installed.  Is this ok?
105         _install_rbenv(repo_cmd_runner, version=version)
106         with in_env(repo_cmd_runner, version):
107             # Need to call this before installing so rbenv's directories are
108             # set up
109             helpers.run_setup_cmd(repo_cmd_runner, ('rbenv', 'init', '-'))
110             if version != 'default':
111                 _install_ruby(repo_cmd_runner, version)
112             # Need to call this after installing to set up the shims
113             helpers.run_setup_cmd(repo_cmd_runner, ('rbenv', 'rehash'))
114             helpers.run_setup_cmd(
115                 repo_cmd_runner,
116                 ('gem', 'build') + repo_cmd_runner.star('.gemspec'),
117             )
118             helpers.run_setup_cmd(
119                 repo_cmd_runner,
120                 (
121                     ('gem', 'install', '--no-ri', '--no-rdoc') +
122                     repo_cmd_runner.star('.gem') + additional_dependencies
123                 ),
124             )
125 
126 
127 def run_hook(repo_cmd_runner, hook, file_args):
128     with in_env(repo_cmd_runner, hook['language_version']):
129         return xargs((hook['entry'],) + tuple(hook['args']), file_args)
130
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/pre_commit/languages/ruby.py b/pre_commit/languages/ruby.py
--- a/pre_commit/languages/ruby.py
+++ b/pre_commit/languages/ruby.py
@@ -19,16 +19,18 @@
 
 
 def get_env_patch(venv, language_version):
-    return (
+    patches = (
         ('GEM_HOME', os.path.join(venv, 'gems')),
         ('RBENV_ROOT', venv),
-        ('RBENV_VERSION', language_version),
         ('PATH', (
             os.path.join(venv, 'gems', 'bin'), os.pathsep,
             os.path.join(venv, 'shims'), os.pathsep,
             os.path.join(venv, 'bin'), os.pathsep, Var('PATH'),
         )),
     )
+    if language_version != 'default':
+        patches += (('RBENV_VERSION', language_version),)
+    return patches
 
 
 @contextlib.contextmanager

{"golden_diff": "diff --git a/pre_commit/languages/ruby.py b/pre_commit/languages/ruby.py\n--- a/pre_commit/languages/ruby.py\n+++ b/pre_commit/languages/ruby.py\n@@ -19,16 +19,18 @@\n \n \n def get_env_patch(venv, language_version):\n-    return (\n+    patches = (\n         ('GEM_HOME', os.path.join(venv, 'gems')),\n         ('RBENV_ROOT', venv),\n-        ('RBENV_VERSION', language_version),\n         ('PATH', (\n             os.path.join(venv, 'gems', 'bin'), os.pathsep,\n             os.path.join(venv, 'shims'), os.pathsep,\n             os.path.join(venv, 'bin'), os.pathsep, Var('PATH'),\n         )),\n     )\n+    if language_version != 'default':\n+        patches += (('RBENV_VERSION', language_version),)\n+    return patches\n \n \n @contextlib.contextmanager\n", "issue": "Ruby hooks failing with rbenv installed\nPre-commit has been failing for the past few weeks.\n\nhttps://gist.github.com/ThatGerber/d6533155848076b25e5e0d5cb02e20eb\n\nSeems to be an issue with the ruby (rbenv) environment.\n\nTried running `pre-commit clean && pre-commit` but it returns the same issue. Setting `rbenv global 2.2.4` and `rbenv shell 2.2.4` does help either.\n\n", "before_files": [{"content": "from __future__ import unicode_literals\n\nimport contextlib\nimport io\nimport os.path\nimport shutil\n\nfrom pre_commit.envcontext import envcontext\nfrom pre_commit.envcontext import Var\nfrom pre_commit.languages import helpers\nfrom pre_commit.util import CalledProcessError\nfrom pre_commit.util import clean_path_on_failure\nfrom pre_commit.util import resource_filename\nfrom pre_commit.util import tarfile_open\nfrom pre_commit.xargs import xargs\n\n\nENVIRONMENT_DIR = 'rbenv'\n\n\ndef get_env_patch(venv, language_version):\n    return (\n        ('GEM_HOME', os.path.join(venv, 'gems')),\n        ('RBENV_ROOT', venv),\n        ('RBENV_VERSION', language_version),\n        ('PATH', (\n            os.path.join(venv, 'gems', 'bin'), os.pathsep,\n            os.path.join(venv, 'shims'), os.pathsep,\n            os.path.join(venv, 'bin'), os.pathsep, Var('PATH'),\n        )),\n    )\n\n\n@contextlib.contextmanager\ndef in_env(repo_cmd_runner, language_version):\n    envdir = os.path.join(\n        repo_cmd_runner.prefix_dir,\n        helpers.environment_dir(ENVIRONMENT_DIR, language_version),\n    )\n    with envcontext(get_env_patch(envdir, language_version)):\n        yield\n\n\ndef _install_rbenv(repo_cmd_runner, version='default'):\n    directory = helpers.environment_dir(ENVIRONMENT_DIR, version)\n\n    with tarfile_open(resource_filename('rbenv.tar.gz')) as tf:\n        tf.extractall(repo_cmd_runner.path('.'))\n    shutil.move(\n        repo_cmd_runner.path('rbenv'), repo_cmd_runner.path(directory),\n    )\n\n    # Only install ruby-build if the version is specified\n    if version != 'default':\n        # ruby-download\n        with tarfile_open(resource_filename('ruby-download.tar.gz')) as tf:\n            tf.extractall(repo_cmd_runner.path(directory, 'plugins'))\n\n        # ruby-build\n        with tarfile_open(resource_filename('ruby-build.tar.gz')) as tf:\n            tf.extractall(repo_cmd_runner.path(directory, 'plugins'))\n\n    activate_path = repo_cmd_runner.path(directory, 'bin', 'activate')\n    with io.open(activate_path, 'w') as activate_file:\n        # This is similar to how you would install rbenv to your home directory\n        # However we do a couple things to make the executables exposed and\n        # configure it to work in our directory.\n        # We also modify the PS1 variable for manual debugging sake.\n        activate_file.write(\n            '#!/usr/bin/env bash\\n'\n            \"export RBENV_ROOT='{0}'\\n\"\n            'export PATH=\"$RBENV_ROOT/bin:$PATH\"\\n'\n            'eval \"$(rbenv init -)\"\\n'\n            'export PS1=\"(rbenv)$PS1\"\\n'\n            # This lets us install gems in an isolated and repeatable\n            # directory\n            \"export GEM_HOME='{0}/gems'\\n\"\n            'export PATH=\"$GEM_HOME/bin:$PATH\"\\n'\n            '\\n'.format(repo_cmd_runner.path(directory))\n        )\n\n        # If we aren't using the system ruby, add a version here\n        if version != 'default':\n            activate_file.write('export RBENV_VERSION=\"{0}\"\\n'.format(version))\n\n\ndef _install_ruby(runner, version):\n    try:\n        helpers.run_setup_cmd(runner, ('rbenv', 'download', version))\n    except CalledProcessError:  # pragma: no cover (usually find with download)\n        # Failed to download from mirror for some reason, build it instead\n        helpers.run_setup_cmd(runner, ('rbenv', 'install', version))\n\n\ndef install_environment(\n        repo_cmd_runner,\n        version='default',\n        additional_dependencies=(),\n):\n    additional_dependencies = tuple(additional_dependencies)\n    directory = helpers.environment_dir(ENVIRONMENT_DIR, version)\n    with clean_path_on_failure(repo_cmd_runner.path(directory)):\n        # TODO: this currently will fail if there's no version specified and\n        # there's no system ruby installed.  Is this ok?\n        _install_rbenv(repo_cmd_runner, version=version)\n        with in_env(repo_cmd_runner, version):\n            # Need to call this before installing so rbenv's directories are\n            # set up\n            helpers.run_setup_cmd(repo_cmd_runner, ('rbenv', 'init', '-'))\n            if version != 'default':\n                _install_ruby(repo_cmd_runner, version)\n            # Need to call this after installing to set up the shims\n            helpers.run_setup_cmd(repo_cmd_runner, ('rbenv', 'rehash'))\n            helpers.run_setup_cmd(\n                repo_cmd_runner,\n                ('gem', 'build') + repo_cmd_runner.star('.gemspec'),\n            )\n            helpers.run_setup_cmd(\n                repo_cmd_runner,\n                (\n                    ('gem', 'install', '--no-ri', '--no-rdoc') +\n                    repo_cmd_runner.star('.gem') + additional_dependencies\n                ),\n            )\n\n\ndef run_hook(repo_cmd_runner, hook, file_args):\n    with in_env(repo_cmd_runner, hook['language_version']):\n        return xargs((hook['entry'],) + tuple(hook['args']), file_args)\n", "path": "pre_commit/languages/ruby.py"}], "after_files": [{"content": "from __future__ import unicode_literals\n\nimport contextlib\nimport io\nimport os.path\nimport shutil\n\nfrom pre_commit.envcontext import envcontext\nfrom pre_commit.envcontext import Var\nfrom pre_commit.languages import helpers\nfrom pre_commit.util import CalledProcessError\nfrom pre_commit.util import clean_path_on_failure\nfrom pre_commit.util import resource_filename\nfrom pre_commit.util import tarfile_open\nfrom pre_commit.xargs import xargs\n\n\nENVIRONMENT_DIR = 'rbenv'\n\n\ndef get_env_patch(venv, language_version):\n    patches = (\n        ('GEM_HOME', os.path.join(venv, 'gems')),\n        ('RBENV_ROOT', venv),\n        ('PATH', (\n            os.path.join(venv, 'gems', 'bin'), os.pathsep,\n            os.path.join(venv, 'shims'), os.pathsep,\n            os.path.join(venv, 'bin'), os.pathsep, Var('PATH'),\n        )),\n    )\n    if language_version != 'default':\n        patches += (('RBENV_VERSION', language_version),)\n    return patches\n\n\n@contextlib.contextmanager\ndef in_env(repo_cmd_runner, language_version):\n    envdir = os.path.join(\n        repo_cmd_runner.prefix_dir,\n        helpers.environment_dir(ENVIRONMENT_DIR, language_version),\n    )\n    with envcontext(get_env_patch(envdir, language_version)):\n        yield\n\n\ndef _install_rbenv(repo_cmd_runner, version='default'):\n    directory = helpers.environment_dir(ENVIRONMENT_DIR, version)\n\n    with tarfile_open(resource_filename('rbenv.tar.gz')) as tf:\n        tf.extractall(repo_cmd_runner.path('.'))\n    shutil.move(\n        repo_cmd_runner.path('rbenv'), repo_cmd_runner.path(directory),\n    )\n\n    # Only install ruby-build if the version is specified\n    if version != 'default':\n        # ruby-download\n        with tarfile_open(resource_filename('ruby-download.tar.gz')) as tf:\n            tf.extractall(repo_cmd_runner.path(directory, 'plugins'))\n\n        # ruby-build\n        with tarfile_open(resource_filename('ruby-build.tar.gz')) as tf:\n            tf.extractall(repo_cmd_runner.path(directory, 'plugins'))\n\n    activate_path = repo_cmd_runner.path(directory, 'bin', 'activate')\n    with io.open(activate_path, 'w') as activate_file:\n        # This is similar to how you would install rbenv to your home directory\n        # However we do a couple things to make the executables exposed and\n        # configure it to work in our directory.\n        # We also modify the PS1 variable for manual debugging sake.\n        activate_file.write(\n            '#!/usr/bin/env bash\\n'\n            \"export RBENV_ROOT='{0}'\\n\"\n            'export PATH=\"$RBENV_ROOT/bin:$PATH\"\\n'\n            'eval \"$(rbenv init -)\"\\n'\n            'export PS1=\"(rbenv)$PS1\"\\n'\n            # This lets us install gems in an isolated and repeatable\n            # directory\n            \"export GEM_HOME='{0}/gems'\\n\"\n            'export PATH=\"$GEM_HOME/bin:$PATH\"\\n'\n            '\\n'.format(repo_cmd_runner.path(directory))\n        )\n\n        # If we aren't using the system ruby, add a version here\n        if version != 'default':\n            activate_file.write('export RBENV_VERSION=\"{0}\"\\n'.format(version))\n\n\ndef _install_ruby(runner, version):\n    try:\n        helpers.run_setup_cmd(runner, ('rbenv', 'download', version))\n    except CalledProcessError:  # pragma: no cover (usually find with download)\n        # Failed to download from mirror for some reason, build it instead\n        helpers.run_setup_cmd(runner, ('rbenv', 'install', version))\n\n\ndef install_environment(\n        repo_cmd_runner,\n        version='default',\n        additional_dependencies=(),\n):\n    additional_dependencies = tuple(additional_dependencies)\n    directory = helpers.environment_dir(ENVIRONMENT_DIR, version)\n    with clean_path_on_failure(repo_cmd_runner.path(directory)):\n        # TODO: this currently will fail if there's no version specified and\n        # there's no system ruby installed.  Is this ok?\n        _install_rbenv(repo_cmd_runner, version=version)\n        with in_env(repo_cmd_runner, version):\n            # Need to call this before installing so rbenv's directories are\n            # set up\n            helpers.run_setup_cmd(repo_cmd_runner, ('rbenv', 'init', '-'))\n            if version != 'default':\n                _install_ruby(repo_cmd_runner, version)\n            # Need to call this after installing to set up the shims\n            helpers.run_setup_cmd(repo_cmd_runner, ('rbenv', 'rehash'))\n            helpers.run_setup_cmd(\n                repo_cmd_runner,\n                ('gem', 'build') + repo_cmd_runner.star('.gemspec'),\n            )\n            helpers.run_setup_cmd(\n                repo_cmd_runner,\n                (\n                    ('gem', 'install', '--no-ri', '--no-rdoc') +\n                    repo_cmd_runner.star('.gem') + additional_dependencies\n                ),\n            )\n\n\ndef run_hook(repo_cmd_runner, hook, file_args):\n    with in_env(repo_cmd_runner, hook['language_version']):\n        return xargs((hook['entry'],) + tuple(hook['args']), file_args)\n", "path": "pre_commit/languages/ruby.py"}]}