考虑以下四个成员函数声明和定义:
// ==== file: x.h
#ifndef X_H
#define X_H
class X {
public:
int a(int i) { return 2 * i; }
inline int b(int i) { return 2 * i; }
int c(int i);
int d(int i);
};
inline int X::c(int i) { return 2 * i; }
int X::d(int i) { return 2 * i; }
#endif
Run Code Online (Sandbox Code Playgroud)
为了完整性,这里是实例化X并调用方法的.cpp文件...
// ==== file: x.cpp
#include "x.h"
#include <stdio.h>
int main() {
X x;
printf("a(3) = %d\n", x.a(3));
printf("b(3) = %d\n", …Run Code Online (Sandbox Code Playgroud) 我想使用CasperJS将表中的连续(内部)HTML字段提取到列表中。我知道从表中提取连续元素属性很容易,但是我不知道如何提取连续HTML字段。
为了演示,这是一个简单的HTML表:
<html>
<head></head>
<body>
<table>
<tbody>
<tr><td name="a">1</td><td>hop</td></tr>
<tr><td name="b">2</td><td>skip</td></tr>
<tr><td name="c">3</td><td>jump</td></tr>
</tbody>
</table>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是一个完整的Casper程序,用于从表中提取位:
"use strict";
var casper = require('casper').create();
casper.start('file:///tmp/casper-so.html');
// I want to print the list '["a", "b", "c"]'
casper.then(function a1() {
var names = casper.getElementsAttribute('table tr td[name]', 'name');
// prints ["a", "b", "c"] as desired...
console.log(JSON.stringify(names, null, 2));
});
// I want to print the list '["hop", "skip", "jump"]'
casper.then(function a2() {
var verbs = ???;
// What should go on …Run Code Online (Sandbox Code Playgroud) 你见过几次这样的事情:
tmp = reference
reference = new_value
return tmp
Run Code Online (Sandbox Code Playgroud)
它并没有真正打扰我,但有没有一些Pythonic速记来完成同样的事情没有临时变量?
(我想象return shift(reference, new_value)......)
我有一个视图助手文件,app/helpers/analysis_helper.rb,我在各种视图文件中使用的顶级方法.工作良好.然后我在analysis_helper.rb中定义了一个AnalysisSummary类来打包一些特定于视图的功能.
但是,当我尝试在视图文件中实例化AnalysisSummary时,我收到错误:
uninitialized constant ActionView::CompiledTemplates::AnalysisSummary
Run Code Online (Sandbox Code Playgroud)
也许Rails告诉我,我不应该在帮助文件中定义一个类?如果是这样,你会在哪里建议停车分析共享?它不是控制器,它不是模型......
谢谢.
我在我的Rails3应用程序中使用Aaron Pfeifer的state_machine gem - 它很漂亮.
如何在当前状态下获取事件列表是合法的?通过这个,我并不是指从当前状态my_model.state_path.events返回所有可传递的事件 - 我只想要那些在当前状态下可用的事件.
我很确定我只是忽略了一些显而易见的事情.
我正在 Rails 应用程序中的包含很多行的表上运行以下迁移:
rake db:migrate
*** [] rake aborted!
*** [] An error has occurred, this and all later migrations canceled:
*** []
*** [] PG::Error: ERROR: deadlock detected
*** [] DETAIL: Process 33319 waits for AccessExclusiveLock on relation 18486 of database 16948; blocked by process 29772.
*** [] Process 29772 waits for ShareLock on transaction 8652; blocked by process 33319.
*** [] HINT: See server log for query details.
*** [] : ALTER TABLE "topics" DROP "most_recent_post_id"
*** [] …Run Code Online (Sandbox Code Playgroud) [旁白:我正在慢慢回答我自己的问题" 在autotest或Guard下安装,配置和运行minitest的简明配方 "]
我的环境:Ruby 2.0.帕德里诺0.10.7.最小2.6.2.RackTest 0.6.2.
简短形式:扩展$ LOAD_PATH以包含我的测试目录的最佳方法是什么,所以我可以简单地require 'test_helper'在我的测试文件中?
长表:
这是一个示例测试文件.注意require_relative "../../../test_helper"- 这需要跟踪相对于test_helper的测试文件.
# file: test/models/api/v0/index_test.rb
require_relative '../../../test_helper'
describe 'nobody home' do
it 'fetch fails' do
get "/api/v0/a_uri_that_does_not_exist"
last_response.status.must_equal 404
end
end
Run Code Online (Sandbox Code Playgroud)
这是测试助手:
# file: test/test_helper.rb
PADRINO_ENV = 'test' unless defined?(PADRINO_ENV)
require File.expand_path('../../config/boot', __FILE__)
class MiniTest::Unit::TestCase
include Rack::Test::Methods
def app
Demo.tap { |app| }
end
end
Run Code Online (Sandbox Code Playgroud)
最后,驱动它的rakefile(由padrino生成,通过调用padrino rake test):
# file: test/test.rake
require 'rake/testtask'
test_tasks = Dir['test/*/'].map { |d| File.basename(d) }
$stderr.puts("=== test_tasks = …Run Code Online (Sandbox Code Playgroud) 我有一个varargs样式的函数,我想拆分为va_list样式的子函数。原始功能:
void container_append(container_t *c, element_t *element, ...) {
element_t *e;
va_list ap;
va_start(ap, element);
while((e = va_arg(ap, element_t *)) != NULL) {
container_append_aux(c, e);
}
va_end(ap);
}
Run Code Online (Sandbox Code Playgroud)
请注意,调用者必须以NULL终止元素列表,但这不会引起任何问题。重构:
void container_append(container_t *c, element_t *element, ...) {
va_list ap;
va_start(ap, element);
container_vappend(c, ap);
va_end(ap);
}
void container_vappend(container_t *c, va_list ap) {
element_t *e;
while ((e = va_arg(ap, element_t *)) != NULL) {
container_append_aux(c, e);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我这样称呼它时:
container_append(c, NULL);
Run Code Online (Sandbox Code Playgroud)
...内container_vappend()的呼叫传va_arg()回非NULL的值。
这是一个更复杂的函数的抄写,但是除非有任何错别字,否则我在设置和使用va_listand时错过了什么va_arg()吗?
假设一个字典,其键是散列值(或任何东西),其元素是共享该散列值的项目列表。一种编码方式:
def add_item(dict_of_lists, item):
key = item.get_key()
if key in dict_of_lists:
# at least one item already maps to this key
dict_of_lists[key].append(item)
else:
# this is the first item to map to this key
dict_of_lists[key] = [item]
Run Code Online (Sandbox Code Playgroud)
有没有更pythonic(或更优雅或更有效)的方法来实现这一点?
python ×2
c ×1
c++ ×1
casperjs ×1
coding-style ×1
dictionary ×1
expression ×1
helpers ×1
javascript ×1
migration ×1
minitest ×1
padrino ×1
postgresql ×1
python-3.x ×1