有时当我将方法名作为参数传递时,我发现自己希望有一个身份函数any_obj.send(:identity) == any_obj,所以不要这样:
(transform.nil?) ? my_obj : my_obj.send(transform)
Run Code Online (Sandbox Code Playgroud)
我可以写
my_obj.send(transform || :identity)
Run Code Online (Sandbox Code Playgroud)
(这是一个人为的例子 - 身份函数可以做的不仅仅是在更复杂的例子中保存一些按键.)
打开Object的定义并添加它会很容易,但是我已经忽略了哪些内容?还有其他人想要这个吗?
PS:我知道我的例子应该说明any_obj.send(:identity).equal?(any_obj),但有时迂腐模糊了这个问题.
我想在控制器测试中使用FactoryGirl.attributes_for,如:
it "raise error creating a new PremiseGroup for this user" do
expect {
post :create, {:premise_group => FactoryGirl.attributes_for(:premise_group)}
}.to raise_error(CanCan::AccessDenied)
end
Run Code Online (Sandbox Code Playgroud)
...但这不起作用,因为#attributes_for省略了:user_id属性.这里的区别#create和#attributes_for:
>> FactoryGirl.create(:premise_group)
=> #<PremiseGroup id: 3, name: "PremiseGroup_4", user_id: 6, is_visible: false, is_open: false)
>> FactoryGirl.attributes_for(:premise_group)
=> {:name=>"PremiseGroup_5", :is_visible=>false, :is_open=>false}
Run Code Online (Sandbox Code Playgroud)
请注意:不存在:user_id #attributes_for.这是预期的行为吗?
FWIW,我的工厂文件包含的定义:premise_group和:user:
FactoryGirl.define do
...
factory :premise_group do
sequence(:name) {|n| "PremiseGroup_#{n}"}
user
is_visible false
is_open false
end
factory :user do
...
end
end
Run Code Online (Sandbox Code Playgroud) 请考虑以下RSpec片段:
it "should match" do
{:a => 1, :b => 2}.should =~ {"a" => 1, "b" => 2}
end
Run Code Online (Sandbox Code Playgroud)
此测试失败,因为一个哈希使用符号作为键,另一个使用字符串作为键.在我的例子中,一个哈希是一个解析的JSON对象,另一个是创建该对象的哈希.我希望他们比较平等.
在我编写自己的匹配器或强制两个哈希来获得字符串键之前,是否存在处理此(常见)案例的匹配器或技术?
何时何OpenSSL::OPENSSL_VERSION_NUMBER地设置?为什么不设置我刚刚安装的最新OpenSSL?
首先是错误:
$ gem install activesupport -v '3.2.13'
Error while executing gem ... (RuntimeError)
Unsupported digest algorithm (SHA512)
Run Code Online (Sandbox Code Playgroud)
如果我直接进入irb,我可以看到Ruby正在使用"旧"openssl:
$ irb
>> require 'openssl'
=> true
>> OpenSSL::Digest.new('sha512')
RuntimeError: Unsupported digest algorithm (sha512)
>> OpenSSL::OPENSSL_VERSION_NUMBER.to_s(16)
"9070cf"
Run Code Online (Sandbox Code Playgroud)
这告诉我Ruby没有找到我刚刚构建的OpenSSL的本地版本,它至少应该是0x908000.相关代码:
# file: usr/lib/ruby/2.0.0/openssl/digest.rb
...
alg = %w(DSS DSS1 MD2 MD4 MD5 MDC2 RIPEMD160 SHA SHA1)
if OPENSSL_VERSION_NUMBER > 0x00908000
alg += %w(SHA224 SHA256 SHA384 SHA512)
end
Run Code Online (Sandbox Code Playgroud)
解释了为什么它没有找到SHA512.
但我不知道为什么Ruby使用旧版本的OpenSSL.我使用的新资源构建了OpenSSL和Ruby
SANDBOX=/Users/me/sandboxes/ruby2
PATH=$(SANDBOX)/usr/bin:$(PATH)
# Create a fresh OpenSSL from sources
(downloaded and unpacked http://www.openssl.org/source/openssl-1.0.1e.tar.gz) …Run Code Online (Sandbox Code Playgroud) 我正在从一个项目的Ruby切换到Python.我很欣赏Python具有一流的功能和闭包,所以这个问题应该很简单.我只是没有弄清楚Python的惯用语是什么:
在Ruby中,我可以写:
def with_quietude(level, &block)
begin
saved_gval = gval
gval = level
yield
ensure
gval = saved_gval
end
end
Run Code Online (Sandbox Code Playgroud)
并称之为:
with_quietude(3) {
razz_the_jazz
begin_the_beguine
}
Run Code Online (Sandbox Code Playgroud)
(注意:我不是在询问Python try/finally处理,也不是关于保存和恢复变量的问题 - 我只想要一个在一些其他代码中包装块的非常重要的例子.)
或者,因为当我真正询问闭包时,有些答案会挂在上一个例子中的全局作业中,如果调用如下,该怎么办?(注意,这不会改变with_quietude的定义):
def frumble(x)
with_quietude {
razz_the_jazz(x)
begin_the_beguine(2 * x)
}
end
Run Code Online (Sandbox Code Playgroud)
你会如何在Python中实现类似的东西(而不是被Python专家嘲笑)?
在RoR应用程序中,我想在我的一个模型中专门化ActiveRecord的update_attributes()方法,提取一些特殊处理的属性,并将其余属性传递给原始的update_attributes()方法.细节:
class Premise < ActiveRecord::Base
...
def update_attributes(attrs)
attrs.each_pair do |key, val|
unless has_attribute?(key)
do_special_processing(key, val)
attrs.delete(key)
end
end
# use original update_attributes() to process non-special pairs
super.update_attributes(attrs)
end
...
end
Run Code Online (Sandbox Code Playgroud)
对super.update_attributes(attr)的调用引发了一个错误:
undefined method `update_attributes' for true:TrueClass
Run Code Online (Sandbox Code Playgroud)
...这让我怀疑我真的不理解Ruby中的super关键字.我错过了什么?具体来说,我如何调用原始的update_attributes()方法?
可能重复:
Rails 3.1和Ruby 1.9.3p125:ruby-debug19仍然崩溃"未找到符号:_ruby_threadptr_data_type"
我已完成打印到控制台 - 我想升级到20世纪并开始使用调试器!但是如何安装ruby-debug?ruby-debug.c当我尝试安装ruby-debug19 gem时,本机编译失败.我查看了其他SO帖子,还没有找到答案......
如果我将ruby-debug19添加到我的Gemfile并执行bundle install,则在构建过程中失败conflicting types for 'rb_iseq_compile_with_option':
# file: Gemfile
...
group :development do
gem 'ruby-debug19'
end
...
% bundle install
...
Installing ruby-debug-base19 (0.11.25) with native extensions
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
/Users/r/Developer/Topaz/usr/bin/ruby extconf.rb
...
ruby_debug.c:29: error: conflicting types for 'rb_iseq_compile_with_option'
$SANDBOX/usr/include/ruby-1.9.1/ruby-1.9.3-p0/vm_core.h:505: error: previous declaration of 'rb_iseq_compile_with_option' was here
...
make: *** [ruby_debug.o] Error 1 …Run Code Online (Sandbox Code Playgroud) 在我的OS X Yosemite(10.10.1)系统上安装puma gem需要什么?我已经用尽了许多途径(我有XCode工具,我有OpenSSL),但在尝试构建原生扩展时仍然失败.
在我的OS X系统上,当我这样做时:
$ gem install puma
Run Code Online (Sandbox Code Playgroud)
我明白了:
Building native extensions. This could take a while...
ERROR: Error installing puma:
ERROR: Failed to build gem native extension.
/Users/home/sandbox/usr/bin/ruby extconf.rb
checking for SSL_CTX_new() in -lssl... no
checking for SSL_CTX_new() in -lssleay32... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options: …Run Code Online (Sandbox Code Playgroud) 序言:我已经阅读了很多SO和博客文章,但没有看到任何回答这个特定问题的内容.也许我只是在寻找错误的东西......
假设我正在开发一个WidgetManager可以操作Widget对象的类.
如何使用sinon测试WidgetManager是否Widget正确使用API而不需要拉入整个Widget库?
基本原理:WidgetManager的测试应该与Widget类分离.也许我还没有编写Widget,或者Widget可能是一个外部库.无论哪种方式,我应该能够测试WidgetManager正确使用Widget的API而无需创建真实的Widgets.
我知道sinon模拟只能在现有的类上工作,据我所知,sinon存根也需要该类存在才能被存根.
为了使它具体化,我将如何测试Widget.create()在下面的代码中使用单个参数'name'调用一次?
// file: widget-manager.js
function WidgetManager() {
this.widgets = []
}
WidgetManager.prototype.addWidget = function(name) {
this.widgets.push(Widget.create(name));
}
Run Code Online (Sandbox Code Playgroud)
// file: widget-manager-test.js
var WidgetManager = require('../lib/widget-manager.js')
var sinon = require('sinon');
describe('WidgetManager', function() {
describe('#addWidget', function() {
it('should call Widget.create with the correct name', function() {
var widget_manager = new WidgetManager();
// what goes here?
});
it('should push one widget onto the widgets list', function() { …Run Code Online (Sandbox Code Playgroud) 注意:有人建议这个问题重复Can I Compare int with size_t direct in C? ,但这里的问题特别询问如何将 size_t 与负值进行比较。因此,应该重新开放。(此外,它还引发了很多深思熟虑的讨论!)
我正在盯着一些库代码(我正在看着你,Microchip),声明返回size_t:
size_t SYS_FS_FileWrite(SYS_FS_HANDLE handle, const void *buf, size_t nbytes);
Run Code Online (Sandbox Code Playgroud)
这被记录为错误时返回 -1。但是size_t一个无符号值。那么,在理论上和/或实践中,以下行为是否允许?
if (SYS_FS_FileWrite(handle, buf, nbytes) == -1) {
report_errror();
}
Run Code Online (Sandbox Code Playgroud) ruby ×6
gem ×2
openssl ×2
c ×1
closures ×1
factory-bot ×1
if-statement ×1
javascript ×1
macos ×1
node.js ×1
puma ×1
python ×1
rspec2 ×1
ruby-debug ×1
sinon ×1
size-t ×1
unit-testing ×1