Tag: ruby

rake db:create – 整理问题

kratos-iii:railsproj zachinglis$ rake db:create (in /Users/zachinglis/Sites/rails/railsproj) Couldn’t create database for {“adapter”=>”mysql”, “host”=>”localhost”, “username”=>”root”, “password”=>nil, “database”=>”railsproj_development”}, charset: utf8, collation: utf8_general_ci (if you set the charset manually, make sure you have a matching collation) 我没有使用Sequel Pro甚至创建所述数据库的问题。 我该如何解决这个问题? 有一个空密码从来没有给我带来问题。 我真的很怀疑它。

rspec –init not working /`mkd ir’:参数无效 – ./C :( Errno :: EINVAL)

我正在关注rspec的 codechool 课程 。 安装工作正常。 我为该项目创建了一个名为rspec-zombie的文件。 精细。 但是当我在powershell中尝试使用rspec –init时,我收到了一个不正常的错误消息。 我的印象是应该在目录中制作一些文件,但不存在。 它跟随。 C:\Users\Roman\The-Odin-Project\rspec-zombie> rspec –init create .rspec C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-support-3.1.0/lib/rspec/support/directory_maker.rb:17:in `mkd ir’: Invalid argument – ./C: (Errno::EINVAL) from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-support-3.1.0/lib/rspec/support/directory_maker. rb:17:in `block in mkdir_p’ from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-support-3.1.0/lib/rspec/support/directory_maker. rb:13:in `each’ from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-support-3.1.0/lib/rspec/support/directory_maker. rb:13:in `mkdir_p’ from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-3.1.3/lib/rspec/core/project_initializer.rb :33:in `copy_template’ from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-3.1.3/lib/rspec/core/project_initializer.rb :22:in `run’ from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-3.1.3/lib/rspec/core/option_parser.rb:81:in `block (2 levels) in parser’ from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/optparse.rb:1360:in […]

如何使用Ruby 2.3中引入的`Array#dig`和`Hash#dig`?

Ruby 2.3在Array和Hash上引入了一种名为dig的新方法。 我在关于新版本的博客文章中看到的例子是人为的,令人费解的: # Hash#dig user = { user: { address: { street1: ‘123 Main street’ } } } user.dig(:user, :address, :street1) # => ‘123 Main street’ # Array#dig results = [[[1, 2, 3]]] results.dig(0, 0, 0) # => 1 我没有使用三嵌套平面数组。 什么是如何有用的现实例子? UPDATE 事实certificate,这些方法解决了一个最常见的Ruby问题。 下面的问题有20个重复,所有这些都是通过使用dig解决的: 如何避免NoMethodError在嵌套哈希中缺少元素,而不重复nil检查? Ruby Style:如何检查嵌套的哈希元素是否存在

确定rails3 jquery自动完成插件的结果范围

我正在使用https://github.com/crowdint/rails3-jquery-autocomplete ,它似乎正在工作。 唯一的问题是假设我有一个字段在所有post标题上执行自动完成,但我希望它只显示用户创建的post标题。 有没有办法做这样的事情? 即find_by还是什么? 谢谢! -Elliot 编辑 它在文档中说: If you want to display a different version of what you’re looking for, you can use the :display_value option. This options receives a method name as the parameter, and that method will be called on the instance when displaying the results. class Brand < ActiveRecord::Base def […]

小写和小写之间的区别! 在Ruby中

我只是学习Ruby而且我不太了解有和没有’!’的几个Ruby方法之间的区别。 在末尾。 有什么不同? 为什么我会使用一个而不是另一个?

将哈希转换为嵌套哈希

这个问题与这个问题相反。 给定一个散列,每个键都有一个数组 { [:a, :b, :c] => 1, [:a, :b, :d] => 2, [:a, :e] => 3, [:f] => 4, } 将它转换为嵌套哈希的最佳方法是什么? { :a => { :b => {:c => 1, :d => 2}, :e => 3, }, :f => 4, }

使用rvm在Xcode Run Script构建阶段强制使用特定的Ruby

在Xcode之外我使用特定版本的Ruby,使用RVM来管理多个Ruby安装。 Apple的命令行开发工具在/usr/bin/ruby安装Ruby,版本为1.8.7。 我通过RVM使用1.9.3。 有没有办法在运行Run Script构建阶段时强制Xcode使用我的1.9.3安装? 我已经尝试将Shell路径设置为我特定Ruby的完整路径,但这似乎没有什么区别,我的意思是我在1.9.3中安装的特定Gems不可用/可见在Xcode中运行时的脚本。 如果我在命令行上通过xcodebuild运行我的项目,则Run Script阶段使用我的特定Ruby,因为它是在我的shell环境中运行的(即使项目文件中的Shell路径设置为/usr/bin/ruby ,它也是如此)仍然使用我的1.9.3)。 我该怎么做才能使IDE使用我的1.9.3 Ruby安装?

为什么我不能使用新的Ruby 1.9.2哈希语法将整数用作键?

Ruby 1.9.2中的新哈希语法意味着我可以执行以下操作: my_hash = {a: 1, b: 2, c: 3} ……相当于: my_hash = {:a => 1, :b => 2, :c => 3} 好的,所以使用旧的语法可以做到这一点(第一个键是一个整数): my_hash = {1 => 1, :b => 2, :c => 3} 而且我发现甚至可以混合使用这样的新语法和旧语法: my_hash = {1 => 1, b: 2, c: 3} 因此,如果我们援引“最少惊喜原则”,人们会认为以下是合法的: my_hash = {1: 1, b: 2, c: 3} ……但事实并非如此。 它会生成语法错误: […]

我可以使用默认值在Ruby中创建数组吗?

Perl对默认值非常好: : jmglov@laurana; perl -e ‘@foo; printf “%d\n”, $foo[123]’ 0 : jmglov@laurana; perl -e ‘%foo; printf “%d\n”, $foo{bar}’ 0 Ruby也可以这样做,至少对于哈希来说是这样的: >> foo = Hash.new(0) => {} >> foo[:bar] => 0 但同样看起来对数组不起作用: >> foo = Array.new(0) => [] >> foo[123] => nil >> foo[124] = 0 => 0 >> foo[456] = 0 => 0 >> foo[455,456] […]

类似正则表达式匹配的xpath表达式?

我想在某个模式的html doc中搜索div id。 我想在正则表达式中匹配此模式: foo_([[:digit:]]{1.8}) 使用xpath。 上述模式的xpath等价物是多少? 我坚持使用//div[@id=”foo_然后是什么?如果有人可以为它继续法律表达。 编辑 对不起,我想我必须详细说明。 实际上它不是foo_ ,它是post_message_ 顺便说一下,我使用mechanize / nokogiri(ruby) 这是片段: html_doc = Nokogiri::HTML(open(myfile)) message_div = html_doc.xpath(‘//div[substring(@id,13) = “post_message_” and substring-after(@id, “post_message_”) => 0 and substring-after(@id, “post_message_”) <= 99999999]') 仍然失败。 错误信息: 无法计算表达式’ //div[substring(@id,13) = “post_message_” and substring-after(@id, “post_message_”) => 0 and substring-after(@id, “post_message_”) <= 99999999] ‘(Nokogiri :: XML :: XPath […]