Tag: ruby

是否有类似于inheritance的类#的钩子只在Ruby类定义之后触发?

在class Foo语句之后#inherited调用#inherited 。 我想要的东西只能在关闭类声明的end语句之后运行。 这里有一些代码来举例说明我的需求: class Class def inherited m puts “In #inherited for #{m}” end end class Foo puts “In Foo” end puts “I really wanted to have #inherited tiggered here.” ### Output: # In #inherited for Foo # In Foo # I really wanted to have #inherited tiggered here. 这样的事情存在吗? 可以创建吗? 我完全没有运气吗?

在小牛上安装任何版本的ruby和RVM

我刚刚升级到osx mavericks,我想安装ruby 2.0,但我得到configure: error: cannot run C compiled programs. 错误。 我用rvm get stable更新rvm,然后键入rvm install 2.0.0并得到一个错误,指示我一个日志文件。 日志文件包含: configure: WARNING: unrecognized options: –without-tcl, –without-tk checking build system type… x86_64-apple-darwin13.0.0 checking host system type… x86_64-apple-darwin13.0.0 checking target system type… x86_64-apple-darwin13.0.0 checking whether the C compiler works… yes checking for C compiler default output file name… a.out checking for […]

Ruby:从字符串中提取单词

我正在尝试从字符串中解析单词并将它们放入数组中。 我尝试过以下的事情: @string1 = “oriented design, decomposition, encapsulation, and testing. Uses ” puts @string1.scan(/\s([^\,\.\s]*)/) 它似乎做了伎俩,但它有点摇摇欲坠(我应该包括更多的特殊字符)。 ruby有更好的方法吗? 可选:我有一个cs课程描述。 我打算从中提取所有单词并将它们放在一个字符串数组中,从生成的数组中删除英语中最常用的单词,然后将其余单词用作用户可用于搜索cs的标记课程。

如何在模型中调用ApplicationController中定义的方法

我在ApplicationController中定义了方法 class ApplicationController < ActionController::Base helper_method :get_active_gateway def get_active_gateway(cart) cart.account.gateways end end 当我在模型中调用此方法时 class Order < ActiveRecord::Base def transfer active= get_active_gateway(self.cart) end end 它抛出错误undefined local variable get_active_gateway 。 所以我写了 class Order < ActiveRecord::Base def transfer active= ApplicationContoller.helpers.get_active_gateway(self.cart) end end 然后它error undefined method nil for Nilclass抛出error undefined method nil for Nilclass 。 我在Rails 3.2.0中工作。

Ruby:在字符处拆分字符串,从右侧算起

简短版 – 如何在ruby中使用Python rsplit()? 更长的版本 – 如果我想在第一个’。’将一个字符串拆分为两个部分(名称,后缀)。 性格,这很好地完成了这项工作: name, suffix = name.split(‘.’, 2) 但如果我想在最后 (最右边)分开’。’ 性格,我还没有能够提出比这更优雅的东西: idx = name.rindex(‘.’) name, suffix = name[0..idx-1], name[idx+1..-1] if idx 请注意,原始名称字符串可能根本没有点,在这种情况下,名称应该是不变的,后缀应该是nil; 它也可能有一个以上的点,在这种情况下,只有最后一个点之后的位应该是后缀。

错误:执行gem …(Zlib :: GzipFile :: Error)时不是gzip格式

我正在开发Sencha touch 2应用程序。 我一直在关注secha触摸应用程序主题的“ 为Sencha Touch应用程序设计用户界面 ”教程。 它需要我安装Ruby,Compass和SASS。 我使用rubyinstaller.org的安装程序安装了Ruby。 执行以下命令时,我得到确认正确安装的预期结果: C:\>ruby -v ruby 1.9.3p327 (2012-11-10) [i386-mingw32] 当前来源是最新的: C:\>gem sources ** CURRENT SOURCES ** http://rubygems.org/ 接下来,由于我在代理后面,我使用以下命令安装HAML / Compass: C:\>gem install -p [proxy:port] compass ERROR: While executing gem … (Zlib::GzipFile::Error) not in gzip format** 有人能帮我吗? 我找到了诸如系统更新,gem源更新等解决方案,但是我的系统上的所有内容都是最新的。 编辑: C:\>gem install compass 在我的私人系统上完美运行。 当我从我的工作场所尝试相同的命令时,我需要使用上面提到的代理,这会导致错误。

将时间转换为其他时区

我有这种格式的日期字符串yyyy-mm-ddThh:mm:ss [Z] 我有一个时区字符串。 例如“Asia / Kolkata” 现在我想将此日期字符串转换为给定时区的时区 例如,如果日期是2014-01-03T23:30:00Z,那么在“Asia / Kolkata”时区它将是2014-01-04T05:00:00。 我尝试使用时间库,但时间库似乎没有任何方法可以转换为其他时区http://ruby-doc.org/core-1.8.6/Time.html#method-c-mktime 。

如何在Ruby中的YAML文件中包含YAML文件

在YAML中是否有自定义标记用于ruby以在YAML文件中包含YAML文件? #Eg: — !include filename: another.yml 前一段时间问了一个类似的问题,没有相关的答案。 我想知道是否有一些类似于Python的自定义标记用于Python。

当参数传递给我的ruby脚本时,为什么会抛出错误?

我正在使用gets来暂停我的脚本输出,直到用户点击回车键。 如果我没有将任何参数传递给我的脚本,那么它可以正常工作。 但是,如果我将任何参数传递给我的脚本,则会因以下错误而死亡: ruby main.rb -i main.rb:74:in `gets’: No such file or directory – -i (Errno::ENOENT) from main.rb:74:in `gets’ … 错误消息显示我传递给脚本的参数。 为什么会看到ARGV? 我正在使用OptionParser来解析我的命令行参数。 如果我使用parse! 而不是parse (所以它删除它从参数列表中解析的东西)然后应用程序工作正常。 所以看起来像是因为某种原因读取了ARGV。 为什么? 这是预期的吗? 有没有办法让它不这样做(做gets()没有帮助)。

为什么alias_method在Rails模型中失败

class Country < ActiveRecord::Base #alias_method :name, :langEN # here fails #alias_method :name=, :langEN= #attr_accessible :name def name; langEN end # here works end 在第一次调用alias_method失败时: NameError: undefined method `langEN’ for class `Country’ 我的意思是当我做例如Country.first时它失败了。 但是在控制台中我可以成功调用Country.first.langEN ,并看到第二个调用也可以。 我错过了什么?