标签: ruby-3

ruby 3.0.1 中的 paypal-checkout-sdk 中断

红宝石3.0.1

轨道 6.1.3.2

执行 Paypal OrdersCreateRequest 失败并出现此错误

NoMethodError (undefined method `escape' for URI:Module):
Run Code Online (Sandbox Code Playgroud)

.rvm/gems/ruby-3.0.1/gems/paypalhttp-1.0.0/lib/paypalhttp/serializers/form_encoded.rb:8:in `block in encode'自 paypalhttp v1.0.0 起,回溯指向包含此行的内容

encoded_params.push("#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}")
Run Code Online (Sandbox Code Playgroud)

显然 URI.escape 方法在 ruby​​ 3 中已被删除,如此处所述https://github.com/ruby/uri/issues/14

有没有解决方法,或者我必须等待 Paypal 更新 gem?我不想降级到 ruby​​ 2.x

paypal ruby-3

6
推荐指数
1
解决办法
555
查看次数

Ruby 3 IRB 主端和 ctrl-u 不工作

我最近开始更多地使用 Ruby 3,似乎 home 键(将我带到行首)、end 键(行尾)和 ctrl-u(清除行)不起作用。

我正在使用 Ruby 3.1.1p18 和 zsh shell 运行 Arch Linux。这些键在 Ruby 2.7 上工作正常。我尝试过 Alacritty 和 xfce4-terminal,两者都有相同的问题。我没有使用 Tmux 或类似的东西。

几年前有一个类似的问题,但那是针对 Windows 的,解决方案没有帮助:Backspace and arrow key isn't work in IRB(Git Bash console) on windows machine

ruby irb ruby-3

6
推荐指数
1
解决办法
521
查看次数

ActiveRecord 对象 ruby​​ 3.0 上的 to_json

我正在使用 rails 6.0.3.6 和 ruby​​ 3.0.0,

当我打电话时,{'user' : User.first }.to_json我得到"{\"user\":\"#<User:0x00007fa0a8dae3c8>\"}"

[User.first, User.last].to_json

如果我切换回 ruby​​ 2.7.2,我会得到正确的结果,即<User:0x00007fa0a8dae3c8>替换为它的所有属性。

知道我缺少什么吗?

ruby-on-rails ruby-3

5
推荐指数
1
解决办法
145
查看次数

如何配置 Steep 来查找 stdlib 之外的 gem 的 RBS 文件?

Cannot find type `Sinatra::Base`
Run Code Online (Sandbox Code Playgroud)

红宝石文件

class StaticApp < Sinatra::Base
end  
Run Code Online (Sandbox Code Playgroud)

.rbs 文件

class StaticApp < Sinatra::Base
end  
Run Code Online (Sandbox Code Playgroud)

跑步

bundle exec steep check --log-level=fatal
Run Code Online (Sandbox Code Playgroud)

结果

[error] Cannot find type `Sinatra::Base`
Diagnostic ID: RBS::UnknownTypeName 
Run Code Online (Sandbox Code Playgroud)

我用steep宝石。看来需要一些文件。但

library 'sinatra' 
Run Code Online (Sandbox Code Playgroud)

不起作用。

#<RBS::EnvironmentLoader::UnknownLibraryError: Cannot find type definitions for library: sinatra ([nil])>
Run Code Online (Sandbox Code Playgroud)

我有什么错吗?谢谢。

ruby ruby-3 rbs steep

5
推荐指数
1
解决办法
1004
查看次数

Rails 6、Ruby 3.0.2 中的acts_as_commentable 的替代品是什么?

我将我们的应用程序升级到 Rails 6.1.4.4 和 Ruby 3.0.2。我有这颗古老的宝石

\n
gem 'acts_as_commentable'\n
Run Code Online (Sandbox Code Playgroud)\n

锁定在 4.0.2 版本。该 gem 似乎不再受支持,这很遗憾,因为当我启动我的应用程序或控制台时,我现在收到此错误

\n
$ rails c\nYour Gemfile lists the gem rspec-rails (>= 0) more than once.\nYou should probably keep only one of them.\nRemove any duplicate entries and specify the gem only once.\nWhile it's not a problem now, it could cause errors if you change the version of one of them later.\n/Users/myuser/.rvm/gems/ruby-3.0.2/gems/hash_dot-2.5.0/lib/hash.rb:19:in `method_missing': undefined method `arity' for {:as=>:commentable, :dependent=>:destroy}:Hash (NoMethodError)\n    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activerecord-6.1.4.4/lib/active_record/associations/builder/association.rb:53:in `build_scope'\n    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activerecord-6.1.4.4/lib/active_record/associations/builder/association.rb:47:in `create_reflection'\n    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activerecord-6.1.4.4/lib/active_record/associations/builder/association.rb:32:in …
Run Code Online (Sandbox Code Playgroud)

monkeypatching ruby-on-rails acts-as-commentable ruby-on-rails-6 ruby-3

5
推荐指数
1
解决办法
636
查看次数

从 ruby​​ 2.7 升级到 3.0 时遇到问题 - 错误:使用未声明的标识符“RUBY_FUNCTION_NAME_STRING”

我已经在努力尝试升级 Rails 3.0 大约一周了。

我不完全确定问题是什么,但我认为这可能是因为我同时拥有 rvm 和 rbenv。

我在这里遵循了 gorails 的升级教程:https ://gorails.com/setup/osx/12-monterey

但每当我跑步时:

$ rbenv install 3.0.3

我收到以下错误:

In file included from compile.c:40:
./vm_callinfo.h:217:16: error: use of undeclared identifier 'RUBY_FUNCTION_NAME_STRING'
    if (debug) rp(ci);
               ^
./internal.h:95:72: note: expanded from macro 'rp'
#define rp(obj) rb_obj_info_dump_loc((VALUE)(obj), __FILE__, __LINE__, RUBY_FUNCTION_NAME_STRING)
Run Code Online (Sandbox Code Playgroud)

这也是我的错误日志的一个例子:

In file included from compile.c:40:
./vm_callinfo.h:217:16: error: use of undeclared identifier 'RUBY_FUNCTION_NAME_STRING'
    if (debug) rp(ci);
               ^
./internal.h:95:72: note: expanded from macro 'rp'
#define rp(obj) rb_obj_info_dump_loc((VALUE)(obj), __FILE__, __LINE__, RUBY_FUNCTION_NAME_STRING)
                                                                       ^
2 errors generated. …
Run Code Online (Sandbox Code Playgroud)

ruby development-environment rbenv ruby-upgrade ruby-3

5
推荐指数
2
解决办法
5167
查看次数

ruby 3 中 URI:Module 的未定义方法“escape”

将我的 Rails 应用程序从 Rails 6.0.1 更新到 Rails 7.0.2.3 后

我遇到了有关 gem“回形针”、“~> 6.1.0”的问题

在应用程序中使用它时出现错误:

ActionView::Template::Error (undefined method `escape' for URI:Module
Did you mean?  escape_once):
Run Code Online (Sandbox Code Playgroud)

在我的应用程序中的用法:

<%= image_tag current_user.image.url('med'), width: "36px" %>
Run Code Online (Sandbox Code Playgroud)

当 ruby​​ gemfile 本身存在错误时如何解决此问题,提前致谢。

paperclip ruby-3 ruby-on-rails-7

5
推荐指数
1
解决办法
2540
查看次数

需要一个处理程序。传递 with: 关键字参数或提供一个块

最近我将我的应用程序从 Ruby 版本 2.6.1 更新到 3.0.1 并且我使用 rbenv 作为版本管理器。

但是当我尝试运行 Rails 服务器时出现错误

=> Booting Puma
=> Rails 6.1.3 application starting in development 
=> Run `bin/rails server --help` for more startup options
Exiting
/home/humayun/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3/lib/active_support/rescuable.rb:56:in `rescue_from': Need a handler. Pass the with: keyword argument or provide a block. (ArgumentError)
    from /home/humayun/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/will_paginate-3.1.8/lib/will_paginate/railtie.rb:67:in `rescue_from'
    from /home/humayun/umerfarooq/Alchemy/app/controllers/application_controller.rb:2:in `<class:ApplicationController>'
    from /home/humayun/umerfarooq/Alchemy/app/controllers/application_controller.rb:1:in `<main>'
Run Code Online (Sandbox Code Playgroud)

我刚刚阅读了有关导致第 56 行错误的函数的信息。

应用程序控制器.rb

 rescue_from Exception, with: :handle_exception
 protect_from_forgery prepend: true, with: :exception
 before_action :configure_permitted_parameters, if: :devise_controller?
 before_action :initialize_api

  def not_found
    raise …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails rbenv ruby-on-rails-6 ruby-3

3
推荐指数
1
解决办法
1263
查看次数

Ruby 3.0 - 参数数量错误(给定 3,预期 1..2)

我们有一个正在使用 uk_postcode gem 的项目。有一个验证器类如下:

class UkPostcodeValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    postcode = UKPostcode.parse(value.to_s)
    return if postcode.full_valid?

    record.errors.add attribute, :invalid_uk_postcode, options
  end
end 
Run Code Online (Sandbox Code Playgroud)

上面的代码在 Ruby 2.7.6 上运行良好,但现在我需要更新到 Ruby 3.0.0。当我这样做时,我们的测试会中断并给出以下错误:

Failure/Error: record.errors.add attribute, :invalid_uk_postcode, options
     
     ArgumentError:
       wrong number of arguments (given 3, expected 1..2) 
Run Code Online (Sandbox Code Playgroud)

我的 Ruby 和 Rails 知识还不是很丰富,但是在网上搜索了很多内容并尝试了不同的东西之后,我发现更改record.errors.add attribute, :invalid_uk_postcode, options可以record.errors.add attribute, :invalid_uk_postcode, **options修复测试。因此,在最后一个参数中添加 ** 可以修复测试,并且验证似乎可以正常工作。从我到目前为止所读到的内容来看,参数似乎必须更加具体,并且通过添加 ** 使其成为关键字参数(我假设它可以采用任何类型/值),但因为我不是作为 Ruby 和 Rails 方面的专家,这更多的是猜测,而不是正确理解这一点。

有人可以更好地指导吗?通过这种方式修复上述更改似乎可以吗?为什么在最后一个参数中添加 ** 可以修复错误问题?

我不确定自动取款机、选项指的是什么以及我将来会研究的内容,但任何知道并可以回答的人将不胜感激。谢谢

网上搜索了一下错误,可以看到Ruby语法有变化。试图理解这一点。

ruby validation ruby-on-rails ruby-3

3
推荐指数
1
解决办法
2875
查看次数

Ruby 3.0 中的 Define_method 问题

我正在从 Ruby 2.7 升级到 3.0,并遇到了关键字参数更改的问题。

之前我们在代码中出于某些目的使用了define_method。但是,随着关键字参数的更改,它不再能够正确处理参数数组。

class Foo
  def test(a: 1, b: 2)
    puts a
    puts b
  end

  old_method = instance_method(:test)

  define_method(:test) do |*args, &block|
    old_method.bind(self).call(*args, &block)
  end
end

Foo.new.test(a: 1)
Run Code Online (Sandbox Code Playgroud)

这将提高 wrong number of arguments (given 1, expected 0) (ArgumentError). 之前它可以在 Ruby 2.7 中运行。我们可以做些什么来让 *args 再次工作吗?

ruby ruby-3

2
推荐指数
1
解决办法
770
查看次数

如何使用 HEREDOC 作为参数传递给方法?

代码示例:

create_data_with(
  first: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
  second: <<~TEXT
    Aenean vel ex bibendum, egestas tortor sit amet, tempus lorem. Ut sit
    amet rhoncus eros. Vestibulum ante ipsum primis in faucibus orci
    luctus et ultrices posuere cubilia curae; Quisque non risus vel lacus
    tristique laoreet. Curabitur quis auctor mauris, nec tempus mauris.
  TEXT,
  third: "Nunc aliquet ipsum at semper sodales."
)
Run Code Online (Sandbox Code Playgroud)

错误出现在这一行:

second: <<~TEXT
Run Code Online (Sandbox Code Playgroud)

RuboCop 是这样描述的:

Lint/Syntax: unterminated string meets end of file
(Using Ruby 3.1 parser; …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-3 ruby-3.1

2
推荐指数
1
解决办法
230
查看次数

rbenv 和苹果 m1。安装 ruby​​ 3.2、2.7 和 2.6 失败

我无法使用 rbenv 安装任何 ruby​​ 版本。我尝试了 2.6.10 2.7.7 或 3.2.0 。

\n

这是我执行时遇到的错误:

\n
\xe2\x9d\xaf rbenv install 3.2.0\nTo follow progress, use 'tail -f /var/folders/4v/kyv3nw1s7lq8s7lz7r6djvsr0000gn/T/ruby-build.20230125170342.85553.log' or pass --verbose\nDownloading openssl-3.0.7.tar.gz...\n-> https://dqw8nmjcqpjn7.cloudfront.net/83049d042a260e696f62406ac5c08bf706fd84383f945cf21bd61e9ed95c396e\nInstalling openssl-3.0.7...\nInstalled openssl-3.0.7 to /Users/idanahal/.rbenv/versions/3.2.0\n\nDownloading ruby-3.2.0.tar.gz...\n-> https://cache.ruby-lang.org/pub/ruby/3.2/ruby-3.2.0.tar.gz\nInstalling ruby-3.2.0...\nruby-build: using readline from homebrew\nruby-build: using gmp from homebrew\n\nBUILD FAILED (macOS 13.1 using ruby-build 20221225)\n\nInspect or clean up the working tree at /var/folders/4v/kyv3nw1s7lq8s7lz7r6djvsr0000gn/T/ruby-build.20230125170342.85553.VSOgmw\nResults logged to /var/folders/4v/kyv3nw1s7lq8s7lz7r6djvsr0000gn/T/ruby-build.20230125170342.85553.log\n\nLast 10 log lines:\ncompiling ossl_x509ext.c\ncompiling ossl_x509name.c\ncompiling ossl_x509req.c\ncompiling ossl_x509revoked.c\ncompiling ossl_x509store.c\ninstalling default openssl libraries\nlinking shared-object date_core.bundle\nlinking shared-object openssl.bundle\nlinking shared-object ripper.bundle\nmake: *** [build-ext] Error …
Run Code Online (Sandbox Code Playgroud)

ruby rbenv ruby-3

2
推荐指数
1
解决办法
9557
查看次数

ruby 升级到 3.0.0 后 Searchkick 搜索方法参数错误

将 ruby​​ 版本从 2.7.2 更新到 3.0.0,默认 Searchkick 搜索行为停止工作。

错误跟踪:

ArgumentError in ProductsAvailabilitiesController#index

wrong number of arguments (given 2, expected 0..1)

searchkick (4.4.4) lib/searchkick/model.rb:43:in `searchkick_search'
...
Run Code Online (Sandbox Code Playgroud)

这是在 ruby​​ 2.7.2 上运行良好的简单搜索方法:

def search_method(query, page)
  search_options = {
    fields: [:name, :address, :comment],
    match: :word_middle,
    page: page,
    per_page: 10
  }

  search(query, search_options)
end
Run Code Online (Sandbox Code Playgroud)

这是我的 Gemfile.lock:

searchkick (4.4.4)
  activemodel (>= 5)
  elasticsearch (>= 6)
  hashie
elasticsearch (7.11.2)
  elasticsearch-api (= 7.11.2)
  elasticsearch-transport (= 7.11.2)
elasticsearch-api (7.11.2)
  multi_json
elasticsearch-transport (7.11.2)
  faraday (~> 1)
  multi_json
Run Code Online (Sandbox Code Playgroud)

有没有办法解决这个问题或者这是宝石来源中的错误?

ruby-on-rails elasticsearch searchkick ruby-3

0
推荐指数
1
解决办法
865
查看次数