postmodern / chruby

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

Version matching based on minor.patch over major.minor #318

Closed tvon closed 4 years ago

tvon commented 9 years ago

chruby will match based on ${minor}.${patch} instead of ${major}.${minor}, e.g. specifying 2.0 will select 2.2.0 instead of 2.0.0, and (for the sake of testing) specifying 1.5 will select 2.1.5. The latter would be a problem if 2.1.9 were to be released and the user only specified 1.9 in their .ruby-version.

tvon@roberto ~ % chruby --version
chruby: 0.3.9
tvon@roberto ~ % chruby
   jruby-1.7.16.1
   rbx-2.2.10
   ruby-1.9.3-p484
   ruby-1.9.3-p547
   ruby-2.0.0-p353
   ruby-2.0.0-p598
   ruby-2.1.3
   ruby-2.1.5
 * ruby-2.2.0
tvon@roberto ~ % chruby 1.9
tvon@roberto ~ % which ruby
/opt/rubies/ruby-1.9.3-p547/bin/ruby
tvon@roberto ~ % chruby 2.0
tvon@roberto ~ % which ruby
/opt/rubies/ruby-2.2.0/bin/ruby
tvon@roberto ~ % chruby 2.0.0
tvon@roberto ~ % which ruby
/opt/rubies/ruby-2.0.0-p598/bin/ruby
tvon@roberto ~ % chruby 1.3
tvon@roberto ~ % which ruby
/opt/rubies/ruby-2.1.3/bin/ruby
tvon commented 9 years ago

The issue:

case "${dir##*/}" in
    "$1")   match="$dir" && break ;;
    *"$1"*) match="$dir" ;;
esac

So if you have ruby-2.1.0 and ruby-2.2.1 then match will match both, but end up being set as the last one it matches. I don't know how to solve this with shell pattern matching, I think you'd want either *-"$1" or ^"$1"* but not *"$1"*