Tag: ruby

升级到OSX Mavericks后修复postgresql

最近升级到OSX Mavericks已经破坏了我的Rails应用程序的数据库连接。 当我尝试从数据库中获取时,服务器返回以下错误: PG::ConnectionBad (could not connect to server: Connection refused Is the server running on host “localhost” (::1) and accepting TCP/IP connections on port 5432? could not connect to server: Connection refused Is the server running on host “localhost” (127.0.0.1) and accepting TCP/IP connections on port 5432? could not connect to server: Connection refused […]

Ruby | =赋值运算符

找到表http://phrogz.net/programmingruby/language.html#table_18.4但无法找到|=描述 |=赋值运算符如何工作?

安装gem时出错

我在安装某些gem时遇到错误。 我正在使用ubuntu 10.10。 错误消息显示了这一点。 ERROR: While executing gem … (NoMethodError) undefined method `spec’ for nil:NilClass

如何编写启动tmux会话的shell脚本,然后运行ruby脚本

我想编写一个执行此操作的shell脚本: 首先,创建一个tmux会话 其次,在tmux会话中运行一个名为“run.rb”的ruby脚本 在伪代码中,我想做什么: tmux new -s my_session ruby run.rb # NOTE: I want this to run inside the my_session tmux session. tmux detach 我该怎么做呢? (我读的post越多,就会越混乱。)

是否可以使用RMagick获得平均图像颜色?

当我将它上传到Ruby on Rails应用程序时,我需要知道图像的平均颜色。 是否可以在HEX或RGB中获得平均颜色值,以便稍后在将要显示此图像的视图中使用此颜色? 就像是: img = Magick::Image.read(path).first hexVal = img.getHexValue

“猴子补丁”真的那么糟糕吗?

像Ruby和JavaScript这样的语言有开放类,允许你修改偶数核心类的接口,如数字,字符串,数组等。显然,这样做可能会使熟悉API的其他人感到困惑,但是有充分的理由要避免使用它们。 ,假设您正在添加到界面而不是更改现有行为? 例如,将一个Array.map实现添加到未实现ECMAScript第5版的Web浏览器(如果您不需要所有jQuery)可能会很好。 或者你的Ruby数组可能会受益于使用“inject”的“sum”方便方法。 只要更改被隔离到您的系统(例如,不是您发布用于分发的软件包的一部分),是否有充分的理由不利用此语言function?

黑客ActiveRecord:添加全局命名范围

我试图为ActiveRecord模型提供一组非常通用的命名范围,如下所示: module Scopes def self.included(base) base.class_eval do named_scope :not_older_than, lambda {|interval| {:conditions => [“#{table_name}.created_at >= ?”, interval.ago] } end end end ActiveRecord::Base.send(:include, Scopes) class User < ActiveRecord::Base end 如果命名范围应该是通用的,我们需要指定* table_name *以防止命名问题,如果它们是来自其他链式命名范围的连接。 问题是我们无法获取table_name,因为它在ActiveRecord :: Base上调用,而不是在User上调用。 User.not_older_than(1.week) NoMethodError: undefined method `abstract_class?’ for Object:Class from /var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2207:in `class_of_active_record_descendant’ from /var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1462:in `base_class’ from /var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1138:in `reset_table_name’ from /var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1134:in `table_name’ from /home/bogdan/makabu/railsware/startwire/repository/lib/core_ext/active_record/base.rb:15:in […]

arrays行为不端

这是代码: # a = Array.new(3, Array.new(3)) a = [[nil,nil,nil],[nil,nil,nil]] a[0][0] = 1 a.each {|line| p line} 随着输出: [1, nil, nil] [nil, nil, nil] 但使用注释行: [1, nil, nil] [1, nil, nil] [1, nil, nil] 那为什么呢?

在rails中解析日期

我有一个日期(实际上是从PDF解析),它可以是以下任何格式: MM/DD/YYYY MM/DD/YY M/D/YY October 15, 2007 Oct 15, 2007 是否有任何gem或function可用rails或ruby来解析我的日期? 或者我需要使用正则表达式解析它? 顺便说一下,我在轨道上使用ruby3.2。

如何使用整数中的前导零

在Ruby中处理前导零的正确方法是什么? 0112.to_s => “74” 0112.to_i => 74 为什么将0112转换为74 ? 如何将0112转换为字符串”0112″ ? 我想定义一个方法,它以整数作为参数,并以数字的降序返回它。 但是当我有前导零时,这对我来说似乎不起作用: def descending_order(n) n.to_s.reverse.to_i end