在Gemfile我的Rails项目中,我开始拥有辅助宝石,如"ruby-debug19","perftools.rb"或"irbtools".所有这些都与项目无关,而是我本地开发设置的一部分.但由于我使用的是bundler,我无法加载这些宝石(即使它们是在系统范围内安装的),除非我将它们添加到Gemfile中.在我看来,这是一个代码味道.
例如,我希望能够require 'irbtools'在rails console不向我的Gemfile添加"irbtools"的情况下使用.
有没有办法将辅助宝石保留在Gemfile之外,并且在我需要时仍能加载它们以进行调试,分析等?
在ruby 1.9.3中,我可以获得字符串的代码点:
> "foo\u00f6".codepoints.to_a
=> [102, 111, 111, 246]
Run Code Online (Sandbox Code Playgroud)
是否有内置方法转向另一个方向,即从整数数组到字符串?
我知道:
# not acceptable; only works with UTF-8
[102, 111, 111, 246].pack("U*")
# works, but not very elegant
[102, 111, 111, 246].inject('') {|s, cp| s << cp }
# concise, but I need to unshift that pesky empty string to "prime" the inject call
['', 102, 111, 111, 246].inject(:<<)
Run Code Online (Sandbox Code Playgroud)
更新(回应尼克拉斯的回答)
有趣的讨论.
pack("U*")始终返回UTF-8字符串,而inject版本返回文件源编码中的字符串.
#!/usr/bin/env ruby
# encoding: iso-8859-1
p [102, 111, 111, 246].inject('', :<<).encoding
p [102, 111, 111, …Run Code Online (Sandbox Code Playgroud) ActiveRecord是否支持where子句中的元组,假设底层数据库有效?
结果where子句看起来像:
where (name, address) in (('John', '123 Main St'))
Run Code Online (Sandbox Code Playgroud)
我试过了:
Person.where({[:name, :address] => ['John', '123 Main St']})
Run Code Online (Sandbox Code Playgroud)
它不起作用.
这些解析并执行正常:
"=".scan(/=/)
"=".scan (/=/)
Run Code Online (Sandbox Code Playgroud)
这导致"未终止的正则表达式符合文件末尾":
"=".scan /=/
Run Code Online (Sandbox Code Playgroud)
如果我=在错误消失之前插入了一些内容:
"=".scan /^=/
Run Code Online (Sandbox Code Playgroud)
这是怎么回事?
Feature: test randomness
In order to make some code testable
As a developer
I want Array#sample to become Array#first
Run Code Online (Sandbox Code Playgroud)
如果有人可以访问Klass.any_instance.stub块中的实例,那么这是可能的.像这样的东西:
Array.any_instance.stub(:sample) { instance.first }
Run Code Online (Sandbox Code Playgroud)
但是那个afaik是不可能的.
无论如何,想要的场景!
我目前正在尝试安装一个失败的ruby gem,因为extconf说它需要libxml,所以当我按照这些说明安装libxml(包括ruby开发工具包安装的这些说明)时,我得到以下输出:
$ gem install libxml-ruby --platform x86-mswin32-60
Temporarily enhancing PATH to include DevKit...
Building native extensions. This could take a while...
ERROR: Error installing libxml-ruby:
ERROR: Failed to build gem native extension.
c:/Ruby/bin/ruby.exe extconf.rb
checking for socket() in -lsocket... no
checking for gethostbyname() in -lnsl... no
checking for atan() in -lm... yes
checking for inflate() in -lz... no
checking for inflate() in -lzlib... no
checking for inflate() in -lzlib1... yes
checking for iconv_open() in -liconv... …Run Code Online (Sandbox Code Playgroud) 有时我会进行正则表达式搜索/.但后来我想使用相同的模式进行替换%s.如何复制或以其他方式重用模式?
当涉及数字文字时,一元减号似乎具有特殊的优先级。这是有记录的行为吗?
我见过的优先级表(例如here)甚至没有提到点(方法调用)运算符。
在 ruby 2.3.6 上测试:
puts "=== literal integer ==="
# `-` has higher precedence than `.`
p( -1.abs ) # => 1
p( -(1.abs) ) # => -1 (previous line should match this if `.` had higher precedence)
puts "=== literal float ==="
# again `-` has higher precedence than `.`
p( -1.2.abs ) # => 1.2
p( -(1.2.abs) ) # => -1.2 (previous line should match this if `.` had higher precedence)
puts "=== integer …Run Code Online (Sandbox Code Playgroud) 场景:
-bash-3.2$ irb -f
ruby-1.9.3-p0 :001 > @v = {}
=> {}
ruby-1.9.3-p0 :002 > def method_missing(sym, *args); @v[sym]; end
=> nil
ruby-1.9.3-p0 :003 > a
(irb):2: stack level too deep (SystemStackError)
-bash-3.2$
Run Code Online (Sandbox Code Playgroud)
我用-f运行以避免加载任何irbrc的东西.当我输入时,我期待得到零a.发生了什么,是否有解决方法?我尝试a用begin/ rescue Exceptionblock 包装,但没有做任何事情.
这也发生在1.9.2,但不是1.9.1.
更奇怪的行为:
-bash-3.2$ irb -f
irb(main):001:0> @v = {}
=> {}
irb(main):002:0> def method_missing(sym, *args); @v[sym]; end; 5.times { p a }
nil
nil
nil
nil
nil
=> 5
irb(main):003:0> a
(irb):2: stack level too deep …Run Code Online (Sandbox Code Playgroud) 使用早期初始化器语法时似乎有一种微妙之处.
trait Base { def callMe = "callMe" }
trait Proxy { this: Base => def call = s"proxied: $callMe" }
val base1 = new Base { } // non-early init works
val baseFail = new { } with Base // error: trait Base is abstract; cannot be instantiated
val base2 = new { val n=1 } with Base // why does this fix the failure?
val proxy = new { } with Base with Proxy // why doesn't this …Run Code Online (Sandbox Code Playgroud) 当我用gem'selenium-webdriver'(带" describe "with valid information", :js => true do")运行测试时
规格/请求/ post_pages_spec.rb
require 'spec_helper'
describe "Post pages" do
subject { page }
category = Category.create(name: "Food")
let(:post) { FactoryGirl.create(:post) }
describe "with valid information", :js => true do
it "should create a post" do
fill_in "Title", with: post.title
fill_in "Content", with: post.content
fill_in "Publish", with: Date.today
check "Food"
expect { click_button "Create Post" }.to change(Post, :count).by(1)
end
end
end
Run Code Online (Sandbox Code Playgroud)
我在规范中遇到错误:
Failures:
1) Post pages post creation with valid information should …Run Code Online (Sandbox Code Playgroud) 此函数无法编译,因为它尝试返回v0已分配到的位置v1.我明白那个.
fn testvec() -> Vec<i64> {
let mut v0 = vec![0,1,2];
let v1 = v0;
//v0 = vec![1,2,3];
v0
}
Run Code Online (Sandbox Code Playgroud)
但如果我取消对v0作业的注释,它就会编译.这背后的原理是什么?有记录吗?
当我第一次发现这种行为时,函数看起来像这样.它的行为与上面相同,但我也想知道嵌套块是否有所不同.
fn testvec() -> Vec<i64> {
let mut v0 = vec![0,1,2];
{
let v1 = v0;
//v0 = vec![1,2,3];
}
v0
}
Run Code Online (Sandbox Code Playgroud)
UPDATE
总结一下我的困惑:我认为"移动"实际上使变量名称无法访问.但实际上只有变量的内容变得无法访问.例如,这个编译器错误具有误导性:v0 moved here because- 听起来变量名称被移动而不仅仅是它的值.