Tag: ruby

Ruby lambda的proc和’instance_eval’

当我将lambda传递给instance_eval作为块时,它似乎传递了一个额外的参数: lamb = -> { puts ‘hi’ } proc = Proc.new { puts ‘hi’ } instance_eval(&lamb) # >> ArgumentError: wrong number of arguments (given 1, expected 0) # from (irb):5:in `block in irb_binding’ # from (irb):7:in `instance_eval’ instance_eval(&proc) # => hi instance_exec(&lamb) # => hi 为什么会这样? 请注意,这个问题不是关于lambda抛出错误的原因。 这很好理解。 问题是关于为什么instance_eval将接收者的self作为参数发送。 它不是必需的,而且令人困惑。 AFAIK没有记录。 这有帮助,但没有解释为什么ruby会这样做。 instance_eval是将self设置为接收者; 为什么通过将self传递给proc来混淆事物呢?

使用Rails学习TDD的最佳资源

我终于掌握了轨道,并且对它开始学习最佳实践感觉很舒服,我的列表中的第一个是TDD原则。 使用Rails了解TDD的最佳资源是什么? 书籍,教程,任何东西。 我渴望学习!

“patch”rails渲染function可编辑默认选项

当我渲染:在rails中的xml我总是想要:dasherize => false选项。 有没有办法将应用程序范围设置为默认值,当然不必修改rails源代码? 也许是一个渲染函数,它以某种方式优先于第一个,然后使用此选项调用它…

安装ffi ruby​​ gem时无法找到ffi.h

Ruby版本2.2.4p230 RubyGem版本2.7.2 安装了Ruby-devel和lib64ffi-devel。 使用64位OpenMandriva 3.0,urpmi和rpmdrake。 目标:安装指南针,但首先需要gemffi。 在过去的两天里,我一直在努力搜索互联网,我很难过……我不可能安装早期版本的ruby,因为它依赖于较旧的ncurses。 我觉得替换ncurses会打开一jar令人讨厌的蠕虫。 但是……我也完全接受罗盘替代方案,其中config.rb项目文件可以以某种方式移植/复制,因此罗盘和其他sass编译器都可以运行。 (这是为了工作,我们不会很快改变sass建设者)。 /usr/share/gems/gems/ffi-1.9.18/ext/ffi_c完全存在,该目录中有一个ffi.c文件,但在附近找不到任何ffi.h文件。 在绝望的时刻,我触摸了该目录中的ffi.h,当我运行gem install时将其删除。 ;)我会尝试取一个别人的libs副本,看看是否有效。 基本上任何方法都可以在不破坏我的其他包的情况下实现这一点。 我不会用ruby做任何其他事情,只需指南针。 这是输出: # sudo gem install ffi Building native extensions. This could take a while… ERROR: Error installing ffi: ERROR: Failed to build gem native extension. current directory: /usr/share/gems/gems/ffi-1.9.18/ext/ffi_c /usr/bin/ruby -r ./siteconf20171110-17988-1lodsok.rb extconf.rb checking for ffi.h… *** extconf.rb failed *** […]

Ruby roo LoadError:无法加载此类文件 – 电子表格/注释

尝试加载xls文件并获取此错误。 有控制台日志: irb(main):001:0> require ‘roo’ => true irb(main):002:0> a = Roo::Excel.new(‘/home/a.xls’) LoadError: cannot load such file — spreadsheet/note from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require’ from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require’ from /var/lib/gems/1.9.1/gems/spreadsheet-0.9.2/lib/spreadsheet/excel/reader.rb:5:in `’ from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require’ from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require’ from /var/lib/gems/1.9.1/gems/spreadsheet-0.9.2/lib/spreadsheet.rb:28:in `’ from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require’ from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require’ from /var/lib/gems/1.9.1/gems/roo-1.12.2/lib/roo/excel.rb:1:in `’ from (irb):2 from /usr/bin/irb:12:in `’ 请帮忙解决这个问题 此外,当我尝试需要电子表格时,我得到相同的错误 require ‘spreadsheet’ 但是这个模块是安装的 […]

Rails3 omniauth google身份validation返回用户身份

我的Omniauth集成适用于本地开发,但谷歌在登台时失败。 require ‘omniauth/openid’ require ‘openid/store/memcache’ Rails.application.config.middleware.use OmniAuth::Builder do OmniAuth.config.full_host = “http://xx.xx.xxx/” # dedicated openid provider :open_id, OpenID::Store::Memcache.new(Dalli::Client.new), :name => ‘google’, :identifier => ‘https://www.google.com/accounts/o8/id’ end 我收到此错误消息: 开始GET“/ auth / failure?message = invalid_credentials”for 58.71.19.178 at 2011-12-01 02:22:20 +0000由ErrorsController处理#routing as HTML参数:{“message”=>“invalid_credentials”,“a “=>”auth / failure“}渲染公共/ 404.html(0.1ms)已完成404未在1ms内找到(视图:0.6ms | ActiveRecord:0.0ms) 我的OmniAuth.config.full_host中的ip in for也不一样可能导致问题?

状态机,模型validation和RSpec

这是我当前的类定义和规范: class Event :not_started do event :game_started do transition :not_started => :in_progress end event :game_ended do transition :in_progress => :final end event :game_postponed do transition [:not_started, :in_progress] => :postponed end state :not_started, :in_progress, :postponed do validate :end_time_before_final end end def end_time_before_final return if end_time.blank? errors.add :end_time, “must be nil until event is final” if end_time.present? […]

Rails 3中的config.logger和config.paths.log

尝试启动服务器时出现此错误。 logger.rb:541:in `exist?’: can’t convert Rails::Paths::Path into String (TypeError) 这是我的development.rb中的代码。 require ‘log_formatter’ config.logger = Logger.new(config.paths.log.first) config.logger.formatter = LogFormatter.new config.logger.level = Logger::DEBUG 我试过添加.to_s,但是没用。 谷歌也没有帮助。

如何使用httparty for rails 3解析og元标记

我试图使用此代码使用HTTParty gem解析og元标记: link = http://www.usatoday.com/story/gameon/2013/01/08/nfl-jets-tony-sparano-fired/1817037/ # link = http://news.yahoo.com/chicago-lottery-winners-death-ruled-homicide-181627271.html resp = HTTParty.get(link) ret_body = resp.body # title og_title = ret_body.match(/\/) og_title = og_title[1].to_s 问题是它在一些网站上工作(雅虎!)而不是其他网站(今天的美国)

使用Ruby Curb传递GET参数

我正在尝试使用Curb(curb.rubyforge.org)来调用需要在get请求中提供的参数的RESTful API。 我想获取像http://foo.com/bar.xml?bla=blablabla这样的url。 我希望能够做类似的事情 Curl::Easy.perform(“http://foo.com/bar.xml”, :bla => ‘blablabla’) {|curl| curl.set_some_headers_if_necessary } 但到目前为止,我能看到的唯一方法是在URL中手动包含?bla=blablabla并自己进行编码。 当然有一种正确的方法可以做到这一点,但我无法弄清楚阅读文档。