Ruby中的类变量

我在本教程中遇到了以下示例: class Song @@plays = 0 def initialize(name, artist, duration) @name = name @artist = artist @duration = duration @plays = 0 end def play @plays += 1 @@plays += 1 “This song: #@plays plays. Total #@@plays plays.” end end s1 = Song.new(“Song1”, “Artist1”, 234) # test songs s2 = Song.new(“Song2”, “Artist2”, 345) puts s1.play […]

Rails查询通过仅限于最新记录的关联?

class User has_many :books 我需要一个返回的查询: 最近一本书的用户:complete => true。 即如果用户最近的书有:complete => false,我不希望它们出现在我的结果中 。 到目前为止我有什么 User.joins(:books).merge(Book.where(:complete => true)) 这是一个很有希望的开始,但没有给我我需要的结果。 我尝试添加.order(“created_on desc”).limit(1) 到上面的查询结束但是当我期待很多时,我最终只得到一个结果。 谢谢!

如何在类上使用哈希键作为方法?

我有一个类和一个哈希。 如何使用键作为方法名称,使哈希的成员动态地成为类的方法? class User def initialize @attributes = {“sn” => “Doe”, “givenName” => “John”} end end 例如,我希望能够有以下输出Doe : u = User.new puts u.sn

如何访问IRB中所需的Ruby文件中定义的变量?

welcome.rb文件包含: welcome_message = “hi there” 但是在IRB中,我无法访问刚刚创建的变量: require ‘./welcome.rb’ puts welcome_message # => undefined local variable or method `welcome_message’ for main:Object 当您在IRB会话中require某些内容时,引入预定义变量并完成初始化工作的最佳方法是什么? 全局变量似乎不是正确的路径。

将数字签名插入现有的pdf文件中

我需要使用rails应用程序服务器将数字签名插入到现有的pdf文件中。 (基本上,客户端上传pdf文件,服务器使用本地证书对其进行签名) 我一直在使用JSignpdf将数字签名插入到pdf文件中,并开始探索ruby的gem…… 我在rubypdf网站http://soft.rubypdf.com/software/pdf-digital-signe上找到了另一个可移植文件,但在ruby中找不到任何gem甚至示例代码。 我也看过OpenSSL的数字签名validation ,但无法理解如何使用本地证书文件对现有文档进行实际签名。 我还在http://code.google.com/p/origami-pdf/上达到了一个高峰,但对于假设“简单”(至少在概念上)的任务来说,这似乎有点苛刻。 有什么想法/建议吗? 谢谢

如何从文本中提取URL

如何从Ruby中的纯文本文件中提取所有URL? 我试过一些库,但在某些情况下它们会失败。 什么是最好的方式?

ruby 1.9 + sinatra不兼容的字符编码:ASCII-8BIT和UTF-8

我正在尝试将sinatra应用程序迁移到ruby 1.9 我正在使用sinatra 1.0,机架1.2.0和erb模板 当我启动sinatra它工作,但当我从浏览器请求网页时,我收到此错误: Encoding::CompatibilityError at / incompatible character encodings: ASCII-8BIT and UTF-8 所有.rb文件都有这个标题: #!/usr/bin/env ruby # encoding: utf-8 我认为问题出现在erb文件中,即使它表明它是UTF-8编码的 [user@localhost views]$ file home.erb home.erb: UTF-8 Unicode text 以前有人有这个问题吗? sinatra与ruby 1.9不完全兼容?

Ruby源代码分析器(类似于pylint)

Ruby是否有任何沿着pylint的工具来分析错误和简单编码标准的源代码? 如果它可以与cruisecontrolrb集成以实现持续集成,那就太好了。 或者每个人都写了这么好的测试,他们不需要源代码检查器!

Rails – 无法运行app:无法加载EventMachine C扩展;

当我在Lion OS X上运行应用程序时,我收到此错误: Unable to load the EventMachine C extension; To use the pure-ruby reactor, require ’em/pure_ruby’ /Users/adam/.rvm/gems/ruby-1.9.2-p290/gems/eventmachine-1.0.0/lib/eventmachine.rb:8:in `require’: no such file to load — rubyeventmachine (LoadError) from /Users/adam/.rvm/gems/ruby-1.9.2-p290/gems/eventmachine-1.0.0/lib/eventmachine.rb:8:in `’ from /Users/adam/.rvm/gems/ruby-1.9.2-p290@global/gems/bundler-1.0.21/lib/bundler/runtime.rb:68:in `require’ from /Users/adam/.rvm/gems/ruby-1.9.2-p290@global/gems/bundler-1.0.21/lib/bundler/runtime.rb:68:in `block (2 levels) in require’ from /Users/adam/.rvm/gems/ruby-1.9.2-p290@global/gems/bundler-1.0.21/lib/bundler/runtime.rb:66:in `each’ from /Users/adam/.rvm/gems/ruby-1.9.2-p290@global/gems/bundler-1.0.21/lib/bundler/runtime.rb:66:in `block in require’ from /Users/adam/.rvm/gems/ruby-1.9.2-p290@global/gems/bundler-1.0.21/lib/bundler/runtime.rb:55:in `each’ from /Users/adam/.rvm/gems/ruby-1.9.2-p290@global/gems/bundler-1.0.21/lib/bundler/runtime.rb:55:in `require’ from […]

使用gmaps4rails动态加载Google Maps Markers

如何仅使用gmaps4rails加载地图边界内的标记? 当然,在平移和/或缩放后加载新的。 与此直接相关,我如何获得地图的当前边界和缩放级别?