无法解救YAML.loadexception

我正在尝试处理在Ruby中加载无效的YAML数据,但似乎无法挽救心理引发的exception。 这是一些示例代码,用于演示我遇到的问题: require ‘yaml’ begin YAML.load(‘&*%^*’) rescue puts “Rescued” end 例外: # ruby test.rb /usr/lib64/ruby/1.9.1/psych.rb:203:in `parse’: (): did not find expected alphabetic or numeric character while scanning an anchor at line 1 column 1 (Psych::SyntaxError) from /usr/lib64/ruby/1.9.1/psych.rb:203:in `parse_stream’ from /usr/lib64/ruby/1.9.1/psych.rb:151:in `parse’ from /usr/lib64/ruby/1.9.1/psych.rb:127:in `load’ from test.rb:3:in `’

动态方法命名

我希望能够动态命名方法(我不会把它留给用户输入来做这个,但作为一个例子): puts “” foo = gets def (whatever the user inputted for foo) end 我怎样才能做到这一点?

在Ruby on Rails中仅接受HTTPS请求

对于某些操作,我只想接受HTTPS请求。 这是我的第一个代码,但我认为有更好的方法。 before_filter :reject_http_request, :only => [:fucntion_a, :function_b] def reject_http_request scheme = request.protocol.to_s.downcase if scheme == ‘http://’ raise AccessDeniedException.new(“Not allowed protocol scheme”) end true end 我该如何改进这段代码?

杰基尔没有发post

我正在使用博客gem在我当前的rails应用程序中放置一个jekyll博客。 基本上,你有一个正常的jekyll构建,但然后你将你的文件放在config / jekyll目录中,并生成文件到公共/博客目录。 但是,当我运行jekyll build ,我的post都没有生成。 这是配置文件: markdown: rdiscount permalink: /:title.html destination: ../../public/blog exclude: – Rakefile – Gemfile – .gitignore 这是我在config / jekyll中的目录结构 ./_config.yml ./_layouts ./_layouts/default.html ./_layouts/page.html ./_layouts/post.html ./_posts ./_posts/2013-06-07-dear-nsa.md ./_posts/2013-06-07-wut.markdown ./atom.xml ./css ./css/screen.css ./css/syntax.css ./index.html 这是生成的目录结构,包含public / blog ./atom.xml ./css ./css/screen.css ./css/syntax.css ./index.html 我想出了一个线索:如果我将源指定为_posts,它将生成我的post的html版本到公共/博客…但不包括css或索引页面。

如何使用rspec测试multithreadingTCPServer

我写了一个非常简单的日期服务器: require ‘socket’ s = TCPServer.new 3939 while (conn = s.accept) Thread.new(conn) do |c| c.print “Enter your name: ” name = c.gets.chomp c.puts “Hi #{name}, the date is…” c.print `date` c.close end end 用户连接,产生一个线程,他们输入他们的名字,返回日期。 简单。 我想知道如何在rspec中测试这样的东西。 我有过的一些想法:1。)使用VCR记录服务器连接,使用Timecop冻结并返回日期。 2.)在之前的块中连接到实际的服务器。 我不完全确定如何执行此操作,因为当我运行rspec时,我认为它实际上运行服务器…或者发生了一些事情,终端只是冻结并等待某些东西……示例测试代码: before @server = TCPSever.new 3939 end it “does something..” conn = @server.accept # etc end after […]

rails中的’deadlock detected’错误

我的代码中检测到死锁错误,并且不明白为什么。 有人可以告诉我,我做错了什么? #!/usr/bin/ruby ENV[‘RAILS_ENV’] = ARGV.first || ENV[‘RAILS_ENV’] || ‘development’ require File.expand_path(File.dirname(__FILE__) + “/config/environment”) mutex = Mutex.new threads = [] 1.upto(10) do |i| threads << Thread.new(i) do |id| mutex.synchronize do # here I want to take 1 record from "class Product < ActiveRecord::Base" Product.first end # and to do here some stuff with it # […]

为什么安装bson_ext会出错?

当我在Rails项目文件夹中执行以下命令时: gem install bson_ext 我收到这个错误: #result Building native extensions. This could take a while… ERROR: Error installing bson_ext: ERROR: Failed to build gem native extension. /home/absolute/.rvm/rubies/ruby-1.9.3-p0/bin/ruby extconf.rb checking for asprintf()… yes checking for ruby/st.h… yes checking for ruby/regex.h… yes checking for ruby/encoding.h… yes creating Makefile make compiling bson_buffer.c compiling cbson.c cbson.c: In function ‘write_element’: cbson.c:439:17: […]

在rails中获取关联的最后一个值

模型Price有一个属性数据,所以 Price.pluck(:data) 结果像[3.99,4.55,5.44] 而Price属于Vendor 所以我想为每个供应商选择最优惠的价格 Vendor.all.pluck(prices.order(“data ASC”).first.data) 在这种情况下,如何为每个供应商提取最低价格数据元素? 提前感谢您的帮助。

Ruby数组哈希键

基本上我正在使用2D矩阵。 我可以通过指定(x,y)对来获取矩阵的元素,以获得该位置的相应值。 现在,我还希望能够跟踪在运行时任意确定的某些对。 例如,我可能需要跟踪(1,2),(3,4)和(5,6)的值,也许我需要经常检索该位置的值。 所以我在考虑如何制作哈希。 likes_elements = {[1,2] => M [1,2],[3,4] => M [3,4],[5,6] => M [5,6]} 或类似的东西。 然后我可以快速迭代哈希并获得我喜欢的元素。 使用数组作为哈希键是否有任何问题?

Ruby模块,带有来自includer类的静态方法调用

我需要在模块中定义使用包含此模块的类中的方法的常量: module B def self.included(base) class << base CONST = self.find end end end class A def self.find "AAA" end include B end puts A::CONST 但是编译器会在第4行给出错误。 有没有其他方法来定义常量?