我需要将Date对象转换为TimeWithZone对象,该对象表示给定时区中那天的开始.
以下方法有效,但似乎太复杂,因为它要求我将日期转换为字符串:
?> date = Date.parse("2010-02-17")
=> Wed, 17 Feb 2010
>> ActiveSupport::TimeZone['Eastern Time (US & Canada)'].parse(date.to_s)
=> Wed, 17 Feb 2010 00:00:00 EST -05:00
>> ActiveSupport::TimeZone['UTC'].parse(date.to_s)
=> Wed, 17 Feb 2010 00:00:00 UTC 00:00
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法让我失踪?
编辑:人们建议的变体:
?> date.to_datetime.in_time_zone('Eastern Time (US & Canada)').beginning_of_day
=> Tue, 16 Feb 2010 00:00:00 EST -05:00
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,这不是一个等价的转换,因为它在美国东部时间2月16日开始,而不是美国东部时间2月17日开始.
我有一个ruby gem,我想在rails active_support模块中包含的gem中使用Hash.from_xml方法.我的gemspec中有以下代码:
gem.add_dependency 'active_support', '~> 3.0.0'
Run Code Online (Sandbox Code Playgroud)
但是,当我在本地构建和安装gem时,运行irb,需要gem,我没有看到包含主动支持的方法?
关于我做错了什么或如何调试的任何建议?谢谢!
我正在开发一个 Rails 应用程序,其中大多数非特定于该应用程序的代码都编写在各种 gem 中,包括一些 Rails 引擎和一些我正在增强或修复错误的 3rd 方 gem。
gem 'mygem', path: File.expath_path('../../mygem', __FILE__)
Run Code Online (Sandbox Code Playgroud)
由于这些 gem 中的许多代码实际上是应用程序的一部分,因此它仍然经常更改。我希望能够利用 Rails 功能,在开发时(即当config.cache_classes
为 false 时)根据每个请求重新加载代码,但这默认情况下仅在正常应用程序结构中完成。
如何配置 Rails 在每个请求上重新加载 gem 代码,就像应用程序代码一样?
我经常发现自己编写Ruby代码,在那里我检查是否存在值,然后在存在的情况下使用该值执行某些操作.例如
if some_object.some_attribute.present?
call_something(some_object.some_attribute)
end
Run Code Online (Sandbox Code Playgroud)
如果它可以写成,我认为它会很酷
some_object.some_attribute.presence { |val| call_something(val) }
=> the return value of call_something
Run Code Online (Sandbox Code Playgroud)
任何人都知道Ruby中是否有这样的功能或者是否支持activesupport?
我打开了此功能的拉取请求.
我正在使用OS X 10.10.5(Yosemite).我正在尝试为MacDown 克隆github repo.README中的说明说克隆后应该这样做
git submodule init
git submodule update
bundle install
bundle exec pod install
Run Code Online (Sandbox Code Playgroud)
我不是一个红宝石程序员,所以我不得不安装Bundler.前两个步骤运行正常,但是当我试图运行时,bundle install
我得到了错误
activesupport-5.0.0.1 requires ruby version >= 2.2.2, which is incompatible with the current version, ruby 2.0.0p481
Run Code Online (Sandbox Code Playgroud)
所以我尝试了brew install ruby
,现在我有了
saul@miniMac ? ruby --version
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin14]
Run Code Online (Sandbox Code Playgroud)
但是,bundle install
给我与以前相同的错误消息.很明显,它正在找到红宝石,/usr/bin/ruby
而不是那个/usr/local/bin/ruby
.我该如何纠正?
我想也许问题是我在升级ruby之前安装了bundler,既没有sudo gem uninstall bundler
也没有sudo gem uninstall bundle
任何影响,我不知道还有什么可以尝试.
以下是所有输出,如果相关:
saul@miniMac ? bundle install
Fetching gem metadata …
Run Code Online (Sandbox Code Playgroud) 我几天来一直在努力解决这个问题.我有一个应用程序,我正在构建一些API,上面提到的错误总是在第一次运行时崩溃我的应用程序.重新加载应用程序时错误消失,但它仍然很烦人.以下是一些有关此错误的类似问题:
ArgumentError:ApplicationController的副本已从模块树中删除但仍处于活动状态
这两个链接都没有解决我面临的问题.这是完整的堆栈跟踪:
ArgumentError (A copy of Api::V1 has been removed from the module tree but is still active!):
activesupport (5.1.4) lib/active_support/dependencies.rb:495:in `load_missing_constant'
activesupport (5.1.4) lib/active_support/dependencies.rb:202:in `const_missing'
activesupport (5.1.4) lib/active_support/inflector/methods.rb:271:in `const_get'
activesupport (5.1.4) lib/active_support/inflector/methods.rb:271:in `block in constantize'
activesupport (5.1.4) lib/active_support/inflector/methods.rb:267:in `each'
activesupport (5.1.4) lib/active_support/inflector/methods.rb:267:in `inject'
activesupport (5.1.4) lib/active_support/inflector/methods.rb:267:in `constantize'
activesupport (5.1.4) lib/active_support/dependencies.rb:582:in `get'
activesupport (5.1.4) lib/active_support/dependencies.rb:613:in `constantize'
actionpack (5.1.4) lib/action_dispatch/http/request.rb:85:in `controller_class_for'
actionpack (5.1.4) lib/action_dispatch/http/parameters.rb:99:in `binary_params_for?'
actionpack (5.1.4) lib/action_dispatch/http/parameters.rb:90:in `set_binary_encoding'
actionpack (5.1.4) lib/action_dispatch/http/parameters.rb:67:in `path_parameters='
actionpack (5.1.4) lib/action_dispatch/journey/router.rb:48:in `block in serve' …
Run Code Online (Sandbox Code Playgroud) 我正在探索ActiveSupport::Notifications
,并希望了解更多信息'instantiation.active_record'
,而不仅仅是:record_count
和:class_name
[1].例如,
ActiveSupport::Notifications.subscribe /instantiation.active_record/ do |*args|
args.status #Database or ActiveRecord return status
args.result #The actual result set returned
args.etc .. #Any other info I can collect on the event
# Additional code ...
end
Run Code Online (Sandbox Code Playgroud)
可以这样做,如果是这样,怎么办?
更新:感谢Tom和Pavel.部分挑战是,我正在构建第三方gem,它将分析主机应用程序的所有SQL返回,而无需修改其代码.基本上,将它插入Gemfile,做一个捆绑,然后离开.
我正在考虑扩展ActiveRecord :: Base,并使用方法append_info_to_payload,但到目前为止还没有在gem中工作.
作为一个例子,我想覆盖ActiveRecord :: Querying find_by_sql()方法的有效负载,如下所示:
payload = {
record_count: result_set.length,
class_name: name,
result_set: result_set
}
Run Code Online (Sandbox Code Playgroud)
在测试和手动编辑我的本地ActiveRecord gem时,它通过ActiveSupport :: Notifications pub/sub提供完整的result_set.
答:所以,答案是使用该class_eval
方法,并在ActiveRecord :: Querying中覆盖find_by_sql方法:
ActiveRecord::Querying.class_eval do
def find_by_sql(sql, binds …
Run Code Online (Sandbox Code Playgroud) 更新到 ActiveSupport 6 时会发生这种情况
start_time = DateTime.now.beginning_of_day
start_time + BigDecimal(2).hours #=> Wed, 11 Sep 2019 01:59:59 +0000
Run Code Online (Sandbox Code Playgroud)
奇怪的是,这与 Time 配合得很好
start_time = Time.now.beginning_of_day
start_time + BigDecimal(2).hours #=> 2019-09-11 02:00:00 +0000
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释为什么?
当我尝试在 Rails 6 中执行任何操作时出现错误(例如数据库迁移、启动服务器、删除数据库等)
我得到的错误是:
ActiveSupport::MessageEncryptor::InvalidMessage: ActiveSupport::MessageEncryptor::InvalidMessage
C:/Users/environment/shafrica/config/environment.rb:5:in `<top (required)>'
Caused by:
OpenSSL::Cipher::CipherError:
C:/Users/environment/shafrica/config/environment.rb:5:in `<top (required)>'
Tasks: TOP => db:migrate => db:load_config => environment
(See full trace by running task with --trace)
Run Code Online (Sandbox Code Playgroud)
我在 Stackoverflow 上四处查看(例如this和this),但他们没有解决我的问题。我觉得它与我的 Postgresql 数据库有关,但我还没有得出任何结论。有人有想法吗?
堆栈跟踪:
49: from bin/rails:4:in `<main>'
48: from bin/rails:4:in `require'
47: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-6.0.2.1/lib/rails/commands.rb:18:in <top (required)>'
46: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-6.0.2.1/lib/rails/command.rb:46:in `invoke'
45: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-6.0.2.1/lib/rails/command/base.rb:69:in `perform'
44: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/thor-0.20.3/lib/thor.rb:387:in `dispatch'
43: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/thor-0.20.3/lib/thor/invocation.rb:126:in `invoke_command'
42: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/thor-0.20.3/lib/thor/command.rb:27:in `run'
41: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-6.0.2.1/lib/rails/commands/server/server_command.rb:138:in `perform'
40: …
Run Code Online (Sandbox Code Playgroud) 你可以做到这两点
def setup(options = {})
options.reverse_merge :size => 25, :velocity => 10
end
Run Code Online (Sandbox Code Playgroud)
和
def setup(options = {})
{ :size => 25, :velocity => 10 }.merge(options)
end
Run Code Online (Sandbox Code Playgroud)
在方法的参数中指定默认值.
问题是:哪一个更好?你更愿意使用哪一个?性能,代码可读性或其他方面有什么不同吗?
编辑:我!
偶然添加了bang()...并不是要问有关无爆炸方法与爆炸方法之间的区别