Ruby无法加载默认的宝石“ random”

时间:2019-11-12 21:34:44

标签: ruby rubygems archlinux

这是我第一次使用Ruby,并且正在使用random。运行ruby main.rb时收到以下错误消息:

Traceback (most recent call last):
    6: from main.rb:3:in `<main>'
    5: from main.rb:3:in `require_relative'
    4: from /home/groctel/Documents/Repositories/civitas-ruby/Civitas/lib/civitas_juego.rb:4:in `<top (required)>'
    3: from /home/groctel/Documents/Repositories/civitas-ruby/Civitas/lib/civitas_juego.rb:4:in `require_relative'
    2: from /home/groctel/Documents/Repositories/civitas-ruby/Civitas/lib/dado.rb:3:in `<top (required)>'
    1: from /usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- random (LoadError)

因此,我认真检查了文件。这是跟踪:

main.rb

# encoding:utf-8

require_relative "civitas_juego.rb"
require_relative "dado.rb"
require_relative "practica3.rb"
require_relative "vista_textual.rb"

# ...

dado.rb

# encoding:utf-8

require 'random'
require 'singleton'
require_relative 'diario'

# ...

错误的最深层原因, kernel_require.rb

  def require(path)
    RUBYGEMS_ACTIVATION_MONITOR.enter

    path = path.to_path if path.respond_to? :to_path

    if spec = Gem.find_unresolved_default_spec(path)
      Gem.remove_unresolved_default_spec(spec)
      begin
        Kernel.send(:gem, spec.name)
      rescue Exception
        RUBYGEMS_ACTIVATION_MONITOR.exit
        raise
      end
    end

    # If there are no unresolved deps, then we can use just try
    # normal require handle loading a gem from the rescue below.

    if Gem::Specification.unresolved_deps.empty?
      RUBYGEMS_ACTIVATION_MONITOR.exit
      return gem_original_require(path) # <- THIS IS LINE 54
    end

我正在使用ruby 2.6.5-1运行Arch Linux。据我了解,from the documentationrandom是一个内核类,而不是我必须自己构建的gem。尽管如此,我还是尝试过gem install random并收到此错误:

Building native extensions. This could take a while...
ERROR:  Error installing random:
    ERROR: Failed to build gem native extension.

    current directory: /home/groctel/.gem/ruby/2.6.0/gems/random-0.2.1/ext/random
/usr/bin/ruby -I /usr/lib/ruby/2.6.0 -r ./siteconf20191112-16451-127m0rj.rb extconf.rb
creating Makefile

current directory: /home/groctel/.gem/ruby/2.6.0/gems/random-0.2.1/ext/random
make "DESTDIR=" clean

current directory: /home/groctel/.gem/ruby/2.6.0/gems/random-0.2.1/ext/random
make "DESTDIR="
compiling mersenne_twister_ext.c
mersenne_twister_ext.c: In function ‘mt_initialize’:
mersenne_twister_ext.c:294:25: error: ‘struct RArray’ has no member named ‘len’
  294 |     if (RARRAY(arg_seed)->len < N)
      |                         ^~
mersenne_twister_ext.c:295:29: error: ‘struct RArray’ has no member named ‘len’
  295 |       max = RARRAY(arg_seed)->len;
      |                             ^~
mersenne_twister_ext.c: At top level:
mersenne_twister_ext.c:246:17: warning: ‘differ’ is static but declared in inline function ‘hash’ which is not static
  246 |   static uint32 differ = 0;  // guarantee time-based seeds will change
      |                 ^~~~~~
make: *** [Makefile:245: mersenne_twister_ext.o] Error 1

make failed, exit code 2

Gem files will remain installed in /home/groctel/.gem/ruby/2.6.0/gems/random-0.2.1 for inspection.
Results logged to /home/groctel/.gem/ruby/2.6.0/extensions/x86_64-linux/2.6.0/random-0.2.1/gem_make.out

此后我不知道该如何进行,看来我走错了方向。 我该怎么做才能加载random

1 个答案:

答案 0 :(得分:4)

您不需要random,因为它已经包含在Ruby中。因此,无需安装gem,也不需要安装任何东西,基本上可以使用它。