在Ruby中使用to_enum创建可枚举对象的优点是什么?

为什么要使用to_enum方法而不是直接使用对象,在Ruby中创建对象的代理引用? 我想不出任何实际用途,试图理解这个概念以及有人可能使用它的地方,但我看到的所有例子看起来都非常微不足道。 例如,为什么使用: “hello”.enum_for(:each_char).map {|c| c.succ } 代替 “hello”.each_char.map {|c| c.succ } 我知道这是一个非常简单的例子,有没有人有任何现实世界的例子?

有没有办法表达“无限时间”?

我使用Range类来表示一系列时间。 现在我想要一个代表给定时间后任意点的范围。 我尝试过(DateTime.now .. nil) ,但是Range类不允许不同类的对象成为相同范围的端点。 (尝试使用此属性创建Range导致ArgumentError: bad value for range 。) 时间是否等于无穷大,即Time.now + (1.0/0.0) ? 或者,有没有办法绕过Range类的要求,即它的min和max是同一个类?

Rails ActionController :: BadRequest在生产服务器上导致500服务器错误

我们有一个土耳其网站,一些旧链接被一些搜索引擎抓取。 链接似乎格式不正确或无法处理,因此导致ActionController::BadRequest错误。 在具有development env的本地计算机上,这会导致返回带有ActionController::BadRequest的Rails错误页面。 但在服务器上,我们得到500 server error 。 这个问题在这里的其他几个页面上讨论过。 但这些解决方案都没有帮助。 在这两种情况下,我们都希望重定向到page not found的page not found -page。 由于前面提到的文章,我已经尝试在ApplicationController尝试rescue_from ActionController::BadRequest和rescue_from ActionController::RoutingError ,其中他们声明BadRequest变成RoutingError 。 但他们都没有奏效。 我希望有人有同样的问题并且已经解决了。 提前感谢您的回答。 编辑: 一个示例问题-url是http://localhost:3000/Di%c5%ef%bf%bd-f%c4%b1r%c3%a7as%c4%b1 。 终端输出: ActionController::BadRequest (ActionController::BadRequest): actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:37:in `block in call’ actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:33:in `each’ actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:33:in `call’ actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call’ actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `each’ actionpack […]

检查答案是否为空

我想为用户创建一个循环,直到他给我一个数字。 但我不知道检查它是数字还是字符串的正确方法。 这样的事情。 puts “Enter your age.” age = gets.chomp.to_i while age.empty? age = gets.chomp.to_i end 空方法导致错误。

如何在初始化父类时返回子类的新实例?

给定一个类层次结构如下: class A def initialize(param) if param == 1 then #initialize and return instance of B else #initialize and return instance of C end end end class B < A end class C < A end 初始化A时是否可以实际初始化并返回B或C的实例? 即my_obj = A.new(param)将导致my_obj成为B类或C类的实例,具体取决于param的值,该值在A.initialize(param)进行检查。 在我的用例中,它在运行时只知道使用哪个子类( B或C )和父类( A )基本上从未真正使用过。 我认为将B或C决定是否为共同祖先的逻辑可能是个好主意。 如果这是不可能的(或不好的风格),我应该在哪里检查param和决定初始化哪个类?

更快速地查看另一个字符串中是否包含大量字符串

我有一个存储在数组中的大约300k常用字的列表。 所以,数组的1个元素= 1个单词。 另一方面,我有一个庞大的字符串列表,其中可能包含其中一个或多个300k字。 示例字符串将是: ifdxawesome453 。 现在,我需要针对常用词检查每个长字符串。 如果在该字符串中找到单词,则立即返回。 所以,我需要再次检查300k字ifdxawesome453并查看其中是否包含任何字。 所以我做的是: huge_list_of_words.any? do |word| random_long_word.include?(word) end 虽然对于随机长词的小样本来说这是可以的,但如果我有数百万个,那么突然需要几个小时才能完成这项工作。 有没有办法更快地做到这一点? 我想到的唯一方法就是如果我抽样出来,说这些300k中最常见的10k字,并先与它进行比较,如果找不到匹配,则与完整列表进行比较。 另一种大幅加速的方法是按大小对300k字的数组进行分组。 当我然后将长随机字与它进行比较时,我首先检查单词的大小是否过滤掉任何更长的单词。 然后我留下相同大小或更少单词的索引,并从具有最小大小的单词开始搜索它们。

使用Rvm安装Ruby 1.9.3时出现问题

我正在尝试使用本指南安装ruby 1.9.3: http : //www.moncefbelyamani.com/how-to-install-xcode-homebrew-git-rvm-ruby-on-mac/ 。 当我运行rvm install 1.9.3我收到此错误 Searching for binary rubies, this might take some time. No binary rubies available for: osx/10.8/x86_64/ruby-1.9.3-p392. Continuing with compilation. Please read ‘rvm mount’ to get more information on binary rubies. Installing requirements for osx, might require sudo password. Skipping `brew update` make sure your formulas are up […]

在rspec中模拟错误/exception(不仅仅是它的类型)

我有一个像这样的代码块: def some_method begin do_some_stuff rescue WWW::Mechanize::ResponseCodeError => e if e.response_code.to_i == 503 handle_the_situation end end end if e.response_code.to_i == 503部分,我想测试一下发生了什么。 我可以模拟do_some_stuff来抛出正确的exception类型: whatever.should_receive(:do_some_stuff).and_raise(WWW::Mechanize::ResponseCodeError) 但是当我收到“response_code”时,如何模拟错误对象本身返回503?

Has_Many:通过或:finder_sql

我已经确定了我想要的东西,但我似乎无法以导轨设计师所寻找的方式获得它。 基本上,我有(请留出多元化/等问题): 人际关系(父母,后代) 我正在努力让所有后代成为一个单亲,以及许多后代的单亲(假设每个后代只有一个父母)。 我可以在模型中以下列方式执行此操作: has_one :parent, :through => :relationships, :foreign_key => :human_id, :source => :source_human has_many :offsprings, :finder_sql => ‘SELECT DISTINCT offsprings.* ‘ + ‘FROM humans offsprings INNER JOIN relationships r on ‘ + ‘r.human_id = offsprings.id where r.source_human_id = #{id}’ 我必须这样做,因为更好的方法: has_many :offsprings, :through => :relationships, :foreign_key => :source_human_id, :source => :human 是不可能的,因为在has_many中忽略了外键(根据这里的文档: […]

服务器不会开始使用authlogic-oauth2

我已经在gemfile中包含了oauth2和authlogic-oauth2,因为我想使用它们并且我正在尝试启动服务器。 它没有启动并给我错误: /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails.rb:44:in `configuration’: undefined method `config’ for nil:NilClass (NoMethodError) from /Library/Ruby/Gems/1.8/gems/authlogic_oauth2-1.1.2/lib/authlogic_oauth2.rb:14 from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:64:in `require’ from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:64:in `require’ from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:62:in `each’ from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:62:in `require’ from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:51:in `each’ from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler/runtime.rb:51:in `require’ from /Library/Ruby/Gems/1.8/gems/bundler-1.0.7/lib/bundler.rb:112:in `require’ from /Users/arkidmitra/Documents/qorm_bzar/buyzaar/config/application.rb:7 from /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/commands.rb:28:in `require’ from /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/commands.rb:28 from /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/commands.rb:27:in `tap’ from /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/commands.rb:27 from script/rails:6:in `require’ from script/rails:6 我使用的是Rails 3.0.3和Ruby 1.8.7。 在我添加之前,服务器似乎也很好 gem […]