Ruby:检查字符串是否可以转换为整数

可能重复: 使用Ruby测试字符串是否基本上是引号中的整数? “1” “一” 第一个字符串是一个数字,我可以说to_i得到一个整数。 第二个字符串也是一个数字,但我不能直接调用to_i来获得所需的数字。 如何检查是否可以使用to_i成功转换?

如何从另一个表中的ID获取名称

我想从另一个表中以某种方式获取company_id的名称。 我试过 没有成功我可以在这里得到一些帮助吗?

在Ruby中测试重叠数组

假设我在Ruby中有一个数组数组, [[100,300], [400,500]] 我正在通过添加连续的CSV数据行来构建。 添加新子arrays时,最好的方法是测试子arrays中两个数字所覆盖的范围是否被任何先前的子arrays覆盖? 换句话说,每个子arrays在上面的示例中包括线性范围(100-300和400-500)。 如果我想要例如将[499,501]添加到数组中,因为会有重叠,我想要抛出exception,我怎么能最好地测试呢?

Ruby:一次从字符串和两个数组值构建哈希

我正在尝试使用以下方法构建哈希: hash = {} strings = [“one”, “two”, “three”] array = [1, 2, 3, 4, 5, 6] 所以我最终得到: hash = { “one” => [1, 2] , “two” => [3, 4] , “three” => [5, 6] } 我试过了: strings.each do |string| array.each_slice(2) do |numbers| hash[string] = [numbers[0], numbers[1]] end end 但是产量: hash = { “one” => […]

UTF-8中的字节序列无效(ArgumentError)

我正在尝试运行Ruby脚本,并且总是在这一行上出错: file_content.gsub(/dr/i,’med’) 我试图用“med”代替“dr”。 错误是: program.rb:4:in `gsub’: invalid byte sequence in UTF-8 (ArgumentError) 为什么,我该如何解决这个问题呢? 我正在使用Ruby 2.2.1p85开发MAC OS X Yosemite机器。

用于集合对象的Rails模型类方法

我在编写用于ActiveRecord对象集合的类方法时遇到了麻烦。 我在最近几个小时内遇到过这个问题两次,这似乎是一个简单的问题,所以我知道我错过了一些东西,但我无法在其他地方找到答案。 例: class Order { where(‘order_date > ?’, DateTime.now.beginning_of_month.utc) } def self.first_order_count map(&:first_for_customer?).count(true) end def first_for_customer? self == customer.orders.first # this self == bit seems awkward, but that’s a separate question… end end 如果我调用Order.month.first_order_count ,我会得到NoMethodError: undefined method ‘map’ for #<Class:… 据我所知,这是因为map不能直接在Order上调用,而是需要一个Enumerable对象。 如果我调用Order.year.map(&:first_for_customer?).count(true) ,我会得到所需的结果。 编写用于ActiveRecord对象集合的方法的正确方法是什么,而不是直接在类上?

Ruby / Mechanize“无法分配内存”。 擦除’agent.get’方法的实例化?

关于在Mechanize Ruby脚本中泄漏内存我有一点问题。 我“while循环”多个网页永远访问,每个循环的内存增加很多。 在分钟后创建了“未能分配内存”并使脚本退出。 事实上,即使我将结果分配给相同的“局部变量”甚至是“全局变量”,似乎agent.get方法agent.get实例化并保存结果。 所以我尝试在最后一次使用之后和重用相同名称变量之前为变量分配nil 。 但似乎以前的agent.get结果仍在内存中,并且真的不知道如何耗尽RAM以使我的脚本在下class后使用大致稳定的内存量? 这是两个代码的和平:( 保持“输入”键并看到Ruby分配的RAM增长) #!/usr/bin/env ruby require ‘mechanize’ agent = Mechanize.new agent.user_agent_alias = ‘Windows Mozilla’ GC.enable #puts GC.malloc_allocations while gets.chomp!=”stop” page = agent.get ‘http://www.nypost.com/’ puts “agent.object_id : “+agent.object_id.to_s puts “page.object_id : “+page.object_id.to_s page=nil puts “page.object_id : “+page.object_id.to_s page = agent.get ‘http://www.nypost.com/’ puts “page.object_id : “+page.object_id.to_s page=nil puts “page.object_id : […]

ActiveModel :: Serializers Gem – Versioned API命名空间问题

我是Rails和模块/命名空间的新手 我的控制器是这样的命名空间: module Api module V1 class PostsController < ApiController ActiveModel :: Serializers在我的app文件夹中放了一个“Serializers”文件夹,在其中,我创建了包含以下代码的post_serializer.rb: class PostSerializer < ActiveModel::Serializer attributes :id, :body, :category, end 当我尝试访问JSON响应时,我得到: NameError at /api/v1/posts uninitialized constant Api::V1::PostsController::PostSerializer 这里的问题是什么?命名空间我的Serializers与API版本一起使用的最佳方法是什么?

使用Nokogiri插入和删除XML节点和元素

我想提取XML文件的一部分,并记下我在该文件中提取了一些部分,比如“这里提取的东西”。 我正试图用Nokogiri这样做,但似乎没有真正记录如何: 删除 更改该完整元素的inner_text 有什么线索吗?

Heroku 301重定向

我怎么做到这一点http://domain.com 301重定向到http://www.domain.com ? 我习惯使用.htaccess来ModRewrite它,但我知道我不能在Heroku上做到这一点。 .htaccess的示例: Options +FollowSymlinks RewriteEngine On RewriteCond %{HTTP_HOST} ^domain.com [NC] RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L] 我的文件结构: – /public — /style — index.html – config.ru 我只是提供单页,我的config.ru包括: use Rack::Static, :urls => [“/style”], :root => “public” run lambda { |env| [ 200, { ‘Content-Type’ => ‘text/html’, ‘Cache-Control’ => ‘public, max-age=86400’ }, File.open(‘public/index.html’, File::RDONLY) ] }