postmodern / chruby

Changes the current Ruby
MIT License
2.87k stars 190 forks source link

Support detection of a .ruby-version file in the root directory. #256

Closed havenwood closed 10 years ago

havenwood commented 10 years ago

The current .ruby-version detection stops one directory shy of the root directory. The loop breaks once it gets to / so /.ruby-version is never discovered. Supporting /.ruby-version just requires running the loop one more time after it reaches the root directory.

Currently $dir reaching the root directory ends the loop:

# Current
local dir="$PWD" version

until [[ -z "$dir" ]]; do
    # detect .ruby-version

    dir="${dir%/*}"
done

One solution is to set $dir before detecting the .ruby-version:

# Option A
local dir="$PWD/" version

until [[ -z "$dir" ]]; do
    dir="${dir%/*}"

    # detect .ruby-version
done

Another option is to switch to a while true loop and have a break condition before before $dir is set:

# Option B
local dir="$PWD" version

while true; do
    # detect .ruby-version

    [[ -z "$dir" ]] && break
    dir="${dir%/*}"
done

I think we should support /.ruby-version both for the sake of consistency and so it can be used as a global default that works outside of the user's home directory.

postmodern commented 10 years ago

Option A seems good.