AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题

问题[ruby](coding)

Martin Hope
teeeeee
Asked: 2025-04-06 04:12:55 +0800 CST

为什么我无法使用此下载的主题在本地构建 Jekyll 网站?

  • 6

我是 Ruby/Jekyll 新手,但我想在 Windows 机器上本地安装 Jekyll,并使用下载的主题构建一个网站。我很难让它运行。我尝试使用的主题是Contrast 主题。我采取的步骤如下:

  1. 按照说明,首先使用此处的Ruby 安装程序安装 Ruby (选择版本Ruby+Devkit 3.3.7-1 [x64])。安装结束时,运行ridk install选项,并安装MSYS2 基础安装以及MSYS2 和 MINGW 开发工具链。
  2. 在命令提示符下运行gem install jekyll bundler,结果如下:
>bundler -v
Bundler version 2.6.7

>gem -v
3.5.22

>jekyll -v
jekyll 4.4.1 
  1. 下载了对比度主题的 zip 文件并解压。解压后得到以下文件:
myuser@mymachine MINGW64 ~/contrast-master
$ ls
_config.yml  _includes/  _posts/  404.html      assets/  Gemfile.lock  README.md
_data/       _layouts/   _sass/   archive.html  Gemfile  index.html    UNLICENSE.txt
  1. 使用命令终端导航到主题的本地目录,然后运行bundler install,安装了一系列所需的 gem:
C:\Users\myuser\contrast-master>bundler install

Fetching gem metadata from https://rubygems.org/...........
Resolving dependencies...
Fetching rexml 3.4.1
Fetching rouge 3.30.0
Fetching unicode-display_width 1.8.0
Fetching sassc 2.4.0
Installing rexml 3.4.1
Installing unicode-display_width 1.8.0
Installing rouge 3.30.0
Fetching terminal-table 2.0.0
Installing sassc 2.4.0 with native extensions
Installing terminal-table 2.0.0
Fetching jekyll-sass-converter 2.2.0
Installing jekyll-sass-converter 2.2.0
Fetching jekyll 4.2.2
Installing jekyll 4.2.2
Fetching jekyll-feed 0.17.0
Installing jekyll-feed 0.17.0
Bundle complete! 3 Gemfile dependencies, 30 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
  1. 尝试使用构建 jekyll 站点bundle exec jekyll build,但失败并显示一系列消息:
C:\Users\myuser\contrast-master>bundle exec jekyll build

C:/Ruby33-x64/bin/jekyll:25: warning: csv was loaded from the standard library, but will no longer be part of the default gems starting from Ruby 3.4.0.
You can add csv to your Gemfile or gemspec to silence this warning.
C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/safe_yaml-1.0.5/lib/safe_yaml/load.rb:22: warning: base64 was loaded from the standard library, but will no longer be part of the default gems starting from Ruby 3.4.0.
You can add base64 to your Gemfile or gemspec to silence this warning.
C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/liquid-4.0.4/lib/liquid.rb:72: warning: bigdecimal was loaded from the standard library, but will no longer be part of the default gems starting from Ruby 3.4.0.
You can add bigdecimal to your Gemfile or gemspec to silence this warning.
                    ------------------------------------------------
      Jekyll 4.2.2   Please append `--trace` to the `build` command
                     for any additional information or backtrace.
                    ------------------------------------------------
C:/Ruby33-x64/lib/ruby/3.3.0/logger.rb:384:in `level': undefined method `[]' for nil (NoMethodError)

    @level_override[Fiber.current] || @level
                   ^^^^^^^^^^^^^^^
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.2.2/lib/jekyll/log_adapter.rb:45:in `adjust_verbosity'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.2.2/lib/jekyll/commands/build.rb:27:in `process'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.2.2/lib/jekyll/command.rb:91:in `block in process_with_graceful_fail'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.2.2/lib/jekyll/command.rb:91:in `each'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.2.2/lib/jekyll/command.rb:91:in `process_with_graceful_fail'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.2.2/lib/jekyll/commands/build.rb:18:in `block (2 levels) in init_with_program'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/mercenary-0.4.0/lib/mercenary/command.rb:221:in `block in execute'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/mercenary-0.4.0/lib/mercenary/command.rb:221:in `each'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/mercenary-0.4.0/lib/mercenary/command.rb:221:in `execute'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/mercenary-0.4.0/lib/mercenary/program.rb:44:in `go'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/mercenary-0.4.0/lib/mercenary.rb:21:in `program'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.2.2/exe/jekyll:15:in `<top (required)>'
        from C:/Ruby33-x64/bin/jekyll:25:in `load'
        from C:/Ruby33-x64/bin/jekyll:25:in `<main>'

有人能帮我看看为什么这无法构建并帮助我修复它吗?


[注意,我想在本地运行它,因为我想要 A)在网站上离线工作,并能够在本地机器的浏览器上进行定期预览,以及 B)访问 _site 文件夹中由 Jekyll 生成的文件,以便将它们直接上传到网络托管服务,而不是让 Jekyll 引擎在 Github Pages、Cloudflare Pages 等上构建网站。]


编辑

按照下面@lmtaq 的回答中的建议,尝试使用较新版本的 jekyll 附加 gem 文件后,我现在收到另一个错误:

$ bundler exec jekyll build
C:/Ruby33-x64/bin/jekyll:25: warning: csv was loaded from the standard library, but will no longer be part of the default gems starting from Ruby 3.4.0.
You can add csv to your Gemfile or gemspec to silence this warning.
C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/safe_yaml-1.0.5/lib/safe_yaml/load.rb:22: warning: base64 was loaded from the standard library, but will no longer be part of the default gems starting from Ruby 3.4.0.
You can add base64 to your Gemfile or gemspec to silence this warning.
To use retry middleware with Faraday v2.0+, install `faraday-retry` gem
Configuration file: C:/Users/tjb/Desktop/Web Development/contrast-master/_config.yml
            Source: C:/Users/tjb/Desktop/Web Development/contrast-master
       Destination: C:/Users/tjb/Desktop/Web Development/contrast-master/_site
 Incremental build: disabled. Enable with --incremental
      Generating...
       Jekyll Feed: Generating feed for posts
Deprecation Warning [import]: Sass @import rules are deprecated and will be removed in Dart Sass 3.0.0.

More info and automated migrator: https://sass-lang.com/d/import

  ╷
1 │ @import index, font, basic, layout, classes
  │         ^^^^^
  ╵
    C:\Users\tjb\Desktop\Web Development\contrast-master\assets\css\frame.sass 1:9  root stylesheet
Deprecation Warning [import]: Sass @import rules are deprecated and will be removed in Dart Sass 3.0.0.

More info and automated migrator: https://sass-lang.com/d/import

  ╷
1 │ @import index, font, basic, layout, classes
  │                ^^^^
  ╵
    C:\Users\tjb\Desktop\Web Development\contrast-master\assets\css\frame.sass 1:16  root stylesheet
Deprecation Warning [import]: Sass @import rules are deprecated and will be removed in Dart Sass 3.0.0.

More info and automated migrator: https://sass-lang.com/d/import

  ╷
1 │ @import index, font, basic, layout, classes
  │                      ^^^^^
  ╵
    C:\Users\tjb\Desktop\Web Development\contrast-master\assets\css\frame.sass 1:22  root stylesheet
Deprecation Warning [import]: Sass @import rules are deprecated and will be removed in Dart Sass 3.0.0.

More info and automated migrator: https://sass-lang.com/d/import

  ╷
1 │ @import index, font, basic, layout, classes
  │                             ^^^^^^
  ╵
    C:\Users\tjb\Desktop\Web Development\contrast-master\assets\css\frame.sass 1:29  root stylesheet
Deprecation Warning [import]: Sass @import rules are deprecated and will be removed in Dart Sass 3.0.0.

More info and automated migrator: https://sass-lang.com/d/import

  ╷
1 │ @import index, font, basic, layout, classes
  │                                     ^^^^^^^
  ╵
    C:\Users\tjb\Desktop\Web Development\contrast-master\assets\css\frame.sass 1:37  root stylesheet
Error: Expected newline.
  ╷
8 │     padding: 1.75em calc(40% - 17em)
  │                                     ^
  ╵
  C:\Users\tjb\Desktop\Web Development\contrast-master\assets\css\index.sass 8:37  @import
  C:\Users\tjb\Desktop\Web Development\contrast-master\assets\css\frame.sass 1:9   root stylesheet
  Conversion error: Jekyll::Converters::Sass encountered an error while converting 'assets/css/frame.sass':
                    Expected newline.
                    ------------------------------------------------
      Jekyll 4.3.3   Please append `--trace` to the `build` command
                     for any additional information or backtrace.
                    ------------------------------------------------
C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-sass-converter-3.1.0/lib/jekyll/converters/scss.rb:181:in `rescue in convert': Expected newline. (Jekyll::Converters::Scss::SyntaxError)

          raise SyntaxError, e.message
                ^^^^^^^^^^^^^^^^^^^^^^
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-sass-converter-3.1.0/lib/jekyll/converters/scss.rb:162:in `convert'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/renderer.rb:105:in `block in convert'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/renderer.rb:104:in `each'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/renderer.rb:104:in `reduce'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/renderer.rb:104:in `convert'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/renderer.rb:84:in `render_document'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/renderer.rb:63:in `run'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/site.rb:572:in `render_regenerated'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/site.rb:564:in `block in render_pages'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/site.rb:563:in `each'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/site.rb:563:in `render_pages'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/site.rb:211:in `render'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/site.rb:80:in `process'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/command.rb:28:in `process_site'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/commands/build.rb:65:in `build'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/commands/build.rb:36:in `process'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/command.rb:91:in `block in process_with_graceful_fail'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/command.rb:91:in `each'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/command.rb:91:in `process_with_graceful_fail'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/commands/build.rb:18:in `block (2 levels) in init_with_program'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/mercenary-0.4.0/lib/mercenary/command.rb:221:in `block in execute'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/mercenary-0.4.0/lib/mercenary/command.rb:221:in `each'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/mercenary-0.4.0/lib/mercenary/command.rb:221:in `execute'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/mercenary-0.4.0/lib/mercenary/program.rb:44:in `go'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/mercenary-0.4.0/lib/mercenary.rb:21:in `program'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/exe/jekyll:15:in `<top (required)>'
        from C:/Ruby33-x64/bin/jekyll:25:in `load'
        from C:/Ruby33-x64/bin/jekyll:25:in `<main>'
C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/sass-embedded-1.86.3-x64-mingw-ucrt/lib/sass/compiler/host.rb:86:in `compile_request': Expected newline. (Sass::CompileError)
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/sass-embedded-1.86.3-x64-mingw-ucrt/lib/sass/compiler.rb:171:in `compile_string'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/sass-embedded-1.86.3-x64-mingw-ucrt/lib/sass/embedded.rb:37:in `compile_string'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-sass-converter-3.1.0/lib/jekyll/converters/scss.rb:163:in `convert'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/renderer.rb:105:in `block in convert'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/renderer.rb:104:in `each'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/renderer.rb:104:in `reduce'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/renderer.rb:104:in `convert'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/renderer.rb:84:in `render_document'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/renderer.rb:63:in `run'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/site.rb:572:in `render_regenerated'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/site.rb:564:in `block in render_pages'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/site.rb:563:in `each'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/site.rb:563:in `render_pages'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/site.rb:211:in `render'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/site.rb:80:in `process'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/command.rb:28:in `process_site'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/commands/build.rb:65:in `build'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/commands/build.rb:36:in `process'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/command.rb:91:in `block in process_with_graceful_fail'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/command.rb:91:in `each'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/command.rb:91:in `process_with_graceful_fail'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/lib/jekyll/commands/build.rb:18:in `block (2 levels) in init_with_program'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/mercenary-0.4.0/lib/mercenary/command.rb:221:in `block in execute'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/mercenary-0.4.0/lib/mercenary/command.rb:221:in `each'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/mercenary-0.4.0/lib/mercenary/command.rb:221:in `execute'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/mercenary-0.4.0/lib/mercenary/program.rb:44:in `go'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/mercenary-0.4.0/lib/mercenary.rb:21:in `program'
        from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/jekyll-4.3.3/exe/jekyll:15:in `<top (required)>'
        from C:/Ruby33-x64/bin/jekyll:25:in `load'
        from C:/Ruby33-x64/bin/jekyll:25:in `<main>'
ruby
  • 1 个回答
  • 59 Views
Martin Hope
tscheingeld
Asked: 2025-04-03 08:42:07 +0800 CST

从分叉进程捕获 STDOUT

  • 5

我想派生一个进程,等待子进程完成,然后从子进程获取 STDOUT。以下代码不起作用:

require 'stringio'

hold_stdout = $stdout
results = $stdout = StringIO.new

if pid = fork()
  Process.wait(pid)
else
  $stdout = hold_stdout
  puts 'stuff in child process'
  exit
end

$stdout = hold_stdout
puts 'results from child:'
puts results.string

输出结果如下:

stuff in child process
results from child:

不知道该做什么。如果你问我“你为什么……?”答案是因为我不知道自己在做什么。任何帮助都值得感激。

ruby
  • 1 个回答
  • 39 Views
Martin Hope
Jellicle
Asked: 2025-04-03 01:21:20 +0800 CST

Ruby Net::SSH 可以像 ControlMaster 那样重用 ssh 连接吗?

  • 5

在我的 中~/.ssh/config,我使用ControlMaster来重用 SSH 连接。Ruby 可以Net::SSH使用ControlMaster或至少利用类似的连接重用机制吗?

我重复使用 SSH 连接,host.example.com因为建立连接需要 MFA,当我需要host.example.com频繁重新访问时,这会变得很麻烦。但这似乎只有在我作为 shell 命令执行时才有效ssh。我希望我工作的 Ruby 项目能够提供相同的功能。

Ruby 项目目前有以下代码:

Net::SSH.start("host.example.com") do |session|
  # ...
end
ruby
  • 1 个回答
  • 27 Views
Martin Hope
tscheingeld
Asked: 2025-02-28 06:17:03 +0800 CST

如何配置 WEBrick 来使用 Unix 套接字?

  • 5

我正在使用 WEBrick 构建一个小型应用程序。它运行良好。但是,它占用了一个端口。我宁愿将其实现为 unix 套接字,但我不知道该怎么做。

我正在使用 WEBrick 版本 1.8.1 和 ruby​​ 3.0.2p107 (2021-07-07 修订版 0db68f0233) [x86_64-linux-gnu]

ruby
  • 1 个回答
  • 31 Views
Martin Hope
tscheingeld
Asked: 2025-02-27 01:30:09 +0800 CST

Ruby 等待独占文件锁

  • 5

我理解文件锁定的一般概念,但恐怕细节我不太清楚。我想打开一个文件进行写入,获取独占锁,但要等待几秒钟才能放弃获取锁。我到目前为止:

File.open(path, 'a') do |file|
  if file.flock(File::LOCK_EX | File::LOCK_NB)
    file.seek(0, IO::SEEK_END) 
    file.write('my content')
  else
    puts "Could not acquire lock, another process is using the file"
  end
end

我如何等待锁x秒?

ruby
  • 2 个回答
  • 63 Views
Martin Hope
tscheingeld
Asked: 2025-02-13 19:01:40 +0800 CST

在确保块中获取退出状态

  • 6

我正在编写一个脚本,该脚本可能会在程序的许多不同点正常退出。该脚本当前始终在确保块中向 STDOUT 输出一条消息,如下所示:

begin
    exit
ensure
    puts "my message"
end

问题是,如果脚本崩溃,消息无论如何都会输出,然后是错误消息。我想要做的是只在退出 0 时输出消息,如下所示:

begin
  exit 1
ensure
  if is_this_a_normal_exit?()
    puts 'my messsage'
  end
end

有办法吗?

ruby
  • 1 个回答
  • 48 Views
Martin Hope
jordy Bolo Caldas
Asked: 2025-02-10 15:02:42 +0800 CST

ActiveRecord::ConnectionNotEstablished 尝试使用本地 PostgreSQL 套接字而不是 Heroku DB

  • 6

我有一个 Rails 8.0.1 应用程序 (Ruby 3.3.4),正在将其部署到 Heroku。代码推送成功,但每当我运行时: heroku run rails db:migrate

我收到此错误:

ActiveRecord::ConnectionNotEstablished: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed

似乎 Rails 正在尝试通过本地 PostgreSQL 套接字进行连接,而不是使用 Heroku 的 Postgres 插件。在本地,使用 rails db:migrate 一切正常。在 Heroku 上,我已经运行了:

heroku addons:create heroku-postgresql:essential-0

…插件创建成功。我的 Gemfile 包括:

gem "rails", "~> 8.0", ">= 8.0.1"
gem "pg", "~> 1.5"
# ...

这是我的数据库.yml:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: brikex2_development

test:
  <<: *default
  database: brikex2_test

production:
  primary: &primary_production
    <<: *default
    database: brikex2_production
    username: brikex2
    password: <%= ENV["BRIKEX2_DATABASE_PASSWORD"] %>
  cache:
    <<: *primary_production
    database: brikex2_production_cache
    migrations_paths: db/cache_migrate
  queue:
    <<: *primary_production
    database: brikex2_production_queue
    migrations_paths: db/queue_migrate
  cable:
    <<: *primary_production
    database: brikex2_production_cable
    migrations_paths: db/cable_migrate

我需要帮助如何正确配置我的项目来解决问题

ruby
  • 1 个回答
  • 26 Views
Martin Hope
tscheingeld
Asked: 2025-01-12 15:28:25 +0800 CST

如何获取有关某个进程(非当前进程)的信息

  • 6

是否有 Ruby 模块或类提供有关当前正在运行但不是当前进程的信息?例如,如果我有 pid 1234,我想获取有关进程用户、时间等的信息。我可以通过 shell 来获取此信息ps,但我更喜欢从 Ruby 获取信息。例如,类似于以下虚构代码:

require 'process/info'
process = ProcessInfo.new(1234)
puts process.pid
puts process.user

任何帮助都值得感激。

ruby
  • 2 个回答
  • 77 Views
Martin Hope
Mike Slinn
Asked: 2025-01-06 20:55:27 +0800 CST

Ruby bsearch 和 bsearch_index 有错误吗?

  • 6

尽管bsearch和bsearch_index对于整数数组来说工作正常,但它们似乎对字符串数组存在问题。

sorted_strings = %w[aaa aab aac bbb bbc bbd ccc ccd cce]
#=> ["aaa", "aab", "aac", "bbb", "bbc", "bbd", "ccc", "ccd", "cce"]
sorted_strings.bsearch_index { |x| x.start_with? 'a' }
#=> nil
sorted_strings.bsearch_index { |x| x.start_with? 'aaa' }
#=> nil
sorted_strings.bsearch_index { |x| x.start_with? 'b' }
#=> 3
sorted_strings.bsearch_index { |x| x.start_with? 'c' }
#=> 6
sorted_strings.bsearch_index { |x| x.start_with? 'cce' }
#=> 8
sorted_strings.bsearch { |x| x.start_with? 'a' }
#=> nil
sorted_strings.bsearch { |x| x.start_with? 'aa' }
#=> nil
sorted_strings.bsearch { |x| x.start_with? 'aaa' }
#=> nil
sorted_strings.bsearch { |x| x.start_with? 'b' }
#=> "bbb"

永远不应该返回 Nil。这是一个错误吗?

与组合比较运算符进行比较时,会返回更疯狂的结果:

sorted_strings = %w[aaa aab aac bbb bbc bbd ccc ccd cce]
#=> ["aaa", "aab", "aac", "bbb", "bbc", "bbd", "ccc", "ccd", "cce"]
sorted_strings.bsearch_index { |x| x <=> 'a' }
#=> nil
sorted_strings.bsearch_index { |x| x <=> 'aaa' }
#=> nil
sorted_strings.bsearch_index { |x| x <=> 'b' }
#=> nil
sorted_strings.bsearch_index { |x| x <=> 'bbb' }
#=> nil
sorted_strings.bsearch_index { |x| x <=> 'bbc' }
#=> 4

鉴于上述方法无效,我不明白为什么下面的方法有效:

sorted_strings = %w[aaa aab aac bbb bbc bbd ccc ccd cce]
#=> ["aaa", "aab", "aac", "bbb", "bbc", "bbd", "ccc", "ccd", "cce"]
sorted_strings.bsearch_index { |x| x >= 'a' }
#=> 0
sorted_strings.bsearch_index { |x| x >= 'aa' }
#=> 0
sorted_strings.bsearch_index { |x| x >= 'aaa' }
#=> 0
sorted_strings.bsearch_index { |x| x >= 'b' }
#=> 3
sorted_strings.bsearch_index { |x| x >= 'bbc' }
#=> 4
sorted_strings.bsearch_index { |x| x >= 'cc' }
#=> 6
sorted_strings.bsearch_index { |x| x >= 'cce' }
#=> 8

这是我使用的 Ruby 版本:

$ ruby -v
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux]
ruby
  • 1 个回答
  • 65 Views
Martin Hope
eem
Asked: 2024-12-13 04:16:33 +0800 CST

为什么需要块的 Ruby 方法可以直接使用 Procs 来代替?

  • 6

我正在学习 Ruby 并查看了Array#map 这里的文档,它说语法如下

map {|element| ... } → new_array
map → new_enumerator

但是我们可以arr.map(&:to_s)对数组的每个元素进行字符串化。据我所知,&:to_s这只是语法糖:to_s.to_proc(如此处所示),这意味着 map 接受一个Proc对象作为参数。但它的函数签名却并非如此。

我对此有几个疑问。

  1. 有人可以解释这种行为并指出相关文档吗?

  2. 在这种情况下到底是什么Proc意思? 这不是to_s底层类的方法吗? 对于我来说,传递一个没有关于将要调用的底层类Proc的信息的对象意味着什么。to_s

任何帮助都将非常有帮助!

ruby
  • 1 个回答
  • 54 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve