从我理解第一个例子的文档String#count,但我不理解其余的例子:
a = "hello world"
a.count "lo" #=> 5
a.count "lo", "o" #=> 2
a.count "hello", "^l" #=> 4
a.count "ej-m" #=> 4
Run Code Online (Sandbox Code Playgroud)
任何解释都会有所帮助.
从Net :: HTTP中拯救异常的最佳方法是什么?
抛出的异常的Ruby的描述socket.c,如Errno::ETIMEDOUT,Errno::ECONNRESET和Errno::ECONNREFUSED.所有这些的基类是SystemCallError,但编写如下代码感觉很奇怪,因为SystemCallError似乎远远没有HTTP打电话:
begin
response = Net::HTTP.get_response(uri)
response.code == "200"
rescue SystemCallError
false
end
Run Code Online (Sandbox Code Playgroud)
只有我吗?除了修复Net::HTTP以处理Errno可能弹出并将其封装在父级中的异常之外,是否有更好的方法来处理此问题HttpRequestException?
在Ruby 1.87我可以这样做:
Date.parse ("3/21/2011")
Run Code Online (Sandbox Code Playgroud)
现在在1.9.2我得到:
ArgumentError:无效的日期
有任何想法吗?
arr = [1,2,1,3,5,2,4]
Run Code Online (Sandbox Code Playgroud)
如何通过排序按组值计算数组?我需要以下输出:
x[1] = 2
x[2] = 2
x[3] = 1
x[4] = 1
x[5] = 1
Run Code Online (Sandbox Code Playgroud) 我有一个Rails 3.0.9应用程序,一旦部署,就会遇到一堆ActiveModel :: MissingAttributeErrors,它会突然出现500s.错误相当随机地发生,有时页面会加载,有时则不加载,但属性是数据库中的所有现有属性,应该找到.
奇怪的是,过了一会儿,错误消失了.突然,他们停止造成问题.
我已经搜索过这个解决方案,但是这个错误主要发生在某人已经完成Model.all(:select => 'column_x,column_y')并且正在调用column_z或者他们正在使用cache_money时.我不做这两件事.
有人可以帮忙吗?
我正在学习木偶并尝试在家里的虚拟机上进行试验.我还没有使用木偶服务器,只是在本地运行.它工作正常,但每次运行时puppet apply ...,我都会延迟几秒钟,然后显示消息
warning: Could not retrieve fact fqdn
Run Code Online (Sandbox Code Playgroud)
我假设消息与延迟有关,我想摆脱它(延迟 - 我可以忍受消息).谷歌搜索解决方案似乎表明它与DNS查找有某种关系,但我真的找不到任何关于它的东西,这似乎令人惊讶.我想要的就是能够快速地在我的vm中应用清单,这样我就可以进行实验.我怎样才能加快速度?
更新:我没有在调试输出中看到任何额外的信息,但它看起来像这样:
$ puppet apply -dv puppet-1.pp
warning: Could not retrieve fact fqdn
debug: Failed to load library 'rubygems' for feature 'rubygems'
debug: Failed to load library 'selinux' for feature 'selinux'
debug: Puppet::Type::File::ProviderMicrosoft_windows: feature microsoft_windows is missing
...
Run Code Online (Sandbox Code Playgroud)
更新:我添加了"ruby"标签,因为木偶的粉丝很少.如果这不属于红宝石,或者您知道更好的标签,请告诉我.
再次更新:我已经了解了更多关于puppet的内容,我现在明白这条消息来自名为"Facter"的组件,该组件嗅出了关于运行Puppet的系统的"事实".我找到了一些配置选项,并使用"certname","node_name"和"node_name_value",但我无法让延迟消失.有没有人具体知道如何告诉Facter忽略fqdn或如何让Facter能够在Ubuntu 11.10 vm上找到fqdn?
进展:
$ cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.1.1
Run Code Online (Sandbox Code Playgroud)
那是我的路由器,它通过Tomato运行Dnsmasq.
$ dig -x 192.168.1.129 192.168.1.1 …Run Code Online (Sandbox Code Playgroud) 当使用pry作为调试器时,我没有看到进入下一行的方法.我能找到的就是编辑方法并将binding.pry移动到下一行.有这样的功能吗?
22: def scan path
23: @last_line_had_text, @files_read, @hands_read = nil, 0, 0
24: Find.find(path){ |file_name|
=> 25: binding.pry
26: read_file(file_name) if file_name.include?(".txt")
27: }
28: end
Run Code Online (Sandbox Code Playgroud) 我的一位开发人员更新了Nokogiri,当拉动更新的Gemfile时,我bundle install失败了.
? my-project git:(master) bundle install
Fetching source index from https://rubygems.org/
Using rake 10.4.2
Using i18n 0.7.0
Using json 1.8.3
Using minitest 5.8.3
Using thread_safe 0.3.5
Using tzinfo 1.2.2
Using activesupport 4.2.3
Using builder 3.2.2
Using erubis 2.7.0
Using mini_portile2 2.0.0
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
/Users/me/.rvm/rubies/ruby-2.1.2/bin/ruby extconf.rb --use-system-libraries
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using system libraries.
libxml2 version 2.6.21 or …Run Code Online (Sandbox Code Playgroud)