首页 > 解决方案 > 如何删除一个 gem 的多个副本?

问题描述

我正在处理我的程序,我注意到它两次打印出此错误消息(尽管已在源代码中修复它,但它继续这样做):

/Users/karenlee/Desktop/app_academy/Ruby/W5D4/minesweeper/board.rb:5: warning: previous definition of BOMB
 was here
board.rb:5: warning: already initialized constant Board::HIDDEN
/Users/karenlee/Desktop/app_academy/Ruby/W5D4/minesweeper/board.rb:5: warning: previous definition of HIDD
EN was here

我做了一些挖掘,发现了这个解决方案:Rails 5.2.0 with Ruby 2.5.1 console - `warning:` `already` initialized constant FileUtils::VERSION

这基本上说明要卸载然后更新 fileutils gem。但是,当我尝试这样做时,我收到此错误消息:

ERROR:  While executing gem ... (Gem::InstallError)
    gem "fileutils" cannot be uninstalled because it is a default gem

我还查看了我的本地宝石列表,我注意到我还有多个其他宝石副本需要清理:

*** LOCAL GEMS ***

bigdecimal (default: 1.4.4, default: 1.3.4)
bundler (2.0.2, default: 1.17.3)
byebug (11.0.1)
cmath (default: 1.0.0)
coderay (1.1.2)
crass (default: 1.0.5)
csv (default: 3.1.2, default: 1.0.0)
date (default: 2.0.0, default: 1.0.0)
dbm (default: 1.0.0)
did_you_mean (default: 1.3.1)
etc (default: 1.0.1, default: 1.0.0)
fcntl (default: 1.0.0)
fiddle (default: 1.0.0)
fileutils (default: 1.3.0, default: 1.0.2)
io-console (default: 0.4.8, default: 0.4.6)
ipaddr (default: 1.2.2, default: 1.2.0)
json (default: 2.2.0, default: 2.1.0)
loofah (default: 2.3.1)
method_source (0.9.2)
minitest (default: 5.13.0)
net-telnet (default: 0.2.0)
nokogiri (default: 1.10.5)
openssl (default: 2.1.2, default: 2.1.0)
power_assert (default: 1.1.5)
pry (0.12.2)
psych (default: 3.1.0, default: 3.0.2)
rake (default: 13.0.0)
rdoc (default: 6.2.0, default: 6.0.1)
scanf (default: 1.0.0)
sdbm (default: 1.0.0)
stringio (default: 0.0.2, default: 0.0.1)
strscan (default: 1.0.3, default: 1.0.0)
test-unit (default: 3.3.4)
tzinfo (default: 2.0.0)
webrick (default: 1.5.0, default: 1.4.2)
zeitwerk (default: 2.2.1)
zlib (default: 1.0.0)

我将如何卸载 fileutils gem(希望这个过程与我拥有的其他副本类似)?如果我无法通过执行“gem uninstall gem_name”来卸载默认 gem,还有其他方法可以做到这一点吗?

编辑 根据下面的评论请求,我将继续在下面发布我的“board.rb”文件:

require_relative "tile"

class Board
    attr_reader :grid
    BOMB, HIDDEN = "", "⬜️"

    # initializes a new board with a grid of spaces
    def initialize
        @grid = self.make_board
    end

    # allows for easy access to positions on the board
    def [](pos)
        row, col = pos
        @grid[row][col]
    end

    # makes a new board (initializes) with the bombs placed
    def make_board
        # see the "new" ruby doc for Arrays; in one form of making a new array,
        # you can use the index as the parameter
        # just using one tile for testing purposes right now
        tile = Tile.new(self, [0, 0])
        # Array.new(9) { | row | Array.new(9) { | col | Tile.new(self, [row, col]) } }
    end

    # randomly places bombs onto the grid
    # def place_bombs
    #     range = (0...@grid.length).to_a
    #     total_bombs = (@grid.length ** 2) / 4
    #     until @grid.flatten.map(&:value).count(BOMB) == total_bombs
    #         @grid[range.sample][range.sample].value = BOMB
    #         # @grid[range.sample][range.sample] = Tile.new(BOMB, self)
    #     end
    # end

    # # checks a position to see if it's a bomb
    # def is_a_bomb?(pos)
    #     row, col = pos
    #     @grid[row][col].value == BOMB
    # end

    # # checks if the entire board of non-bomb tiles are flipped over
    # def won?
    #     safe_tiles = @grid.flatten.select { | tile | tile.value == SAFE }
    #     safe_tiles.all? { | tile | tile.revealed }
    # end

    # # prints out board to the user
    # def render
    #     puts "   #{(0..8).to_a.join("   ")}"
    #     puts
    #     @grid.each.with_index do | row, row_indx |
    #         print "#{row_indx}  "
    #         row.each do | tile |
    #             if tile.revealed
    #                 print "#{tile.value}   "
    #             else  
    #                 print "#{HIDDEN}   "
    #             end
    #         end
    #         puts
    #         puts
    #     end
    # end
end

# board = Board.new
# p require_relative "tile"
# p board.grid

至于我如何运行程序,我正在使用 Visual Studio Code 并通过在 Visual Studio 内提供的终端屏幕的命令行上键入“ruby board.rb”来运行它。

标签: rubyrubygems

解决方案


如果我是你,我会删除我所有的宝石。

gem uninstall -aIx

然后创建一个名为 .ruby-version 的新文件

ruby-2.6.4

和另一个名为 .ruby-gemset 的文件

app_academy

然后安装 rvm,并使用gemset和 Bundler 来管理依赖项。


推荐阅读