有一个公共类方法来向机械化表单添加字段
我试过了 ..
#login_form.field.new('auth_login','Login')
#login_form.field.new('auth_login','Login')
Run Code Online (Sandbox Code Playgroud)
两者都给了我一个错误 undefined method "new" for #<WWW::Mechanize::Form::Field:0x3683cbc> (NoMethodError)
我试过login_form.field.new('auth_login','Login')给了我一个错误
mechanize-0.9.3/lib/www/mechanize/page.rb:13 n `meta': undefined method `search' for nil:NilClass (NoMethodError)
Run Code Online (Sandbox Code Playgroud)
但是在我提交表格时.该字段在html源中不存在.我想添加它,所以我的脚本发送的POST查询将包含auth_username=myusername&auth_password=mypassword&auth_login=Login到目前为止它只发送auth_username=radek&auth_password=mypassword可能是我无法登录的原因.只是我的想法.
脚本看起来像
require 'rubygems'
require 'mechanize'
require 'logger'
agent = WWW::Mechanize.new {|a| a.log = Logger.new("loginYOTA.log") }
agent.follow_meta_refresh = true #Mechanize does not follow meta refreshes by default, we need to set that option.
page = agent.get("http://www.somedomain.com/login?auth_successurl=http://www.somedomain.com/forum/yota?baz_r=1")
login_form = page.form_with(:method => 'POST')
puts login_form.buttons.inspect
puts page.forms.inspect
#STDIN.gets
login_form.fields.each { |f| puts …Run Code Online (Sandbox Code Playgroud) 我想从我的SVN中排除一个目录(我正在使用Xcode内置的SCM).它没有签入,但我只是厌倦了从签入中取消选择.
我的大多数SVN经验都是在Windows上使用TortoiseSVN,它具有"忽略"功能; 我认为SCM有相同的选择.
默认情况下,从getter函数返回副本(1)或引用(2)会更好吗?
class foo {
public:
std::string str () { // (1)
return str_;
}
const std::string& str () { // (2)
return str_;
}
private:
std::string str_;
};
Run Code Online (Sandbox Code Playgroud)
我知道2)可能更快,但不必因(N)RVO.1)关于悬挂引用更安全,但对象可能会过时或永远不会存储引用.
当你写一个课程时,你的默认值是什么,而且还不知道(但)性能和生命周期问题是否重要?
附加问题:当成员不是普通字符串而是向量时,游戏会改变吗?
使用accepts_nested_attributes_for时如何编辑连接模型的属性?
我有3个模型:由连接器加入的主题和文章
class Topic < ActiveRecord::Base
has_many :linkers
has_many :articles, :through => :linkers, :foreign_key => :article_id
accepts_nested_attributes_for :articles
end
class Article < ActiveRecord::Base
has_many :linkers
has_many :topics, :through => :linkers, :foreign_key => :topic_id
end
class Linker < ActiveRecord::Base
#this is the join model, has extra attributes like "relevance"
belongs_to :topic
belongs_to :article
end
Run Code Online (Sandbox Code Playgroud)
所以当我在主题控制器的"新"动作中构建文章时......
@topic.articles.build
Run Code Online (Sandbox Code Playgroud)
...并在topics/new.html.erb中创建嵌套表单...
<% form_for(@topic) do |topic_form| %>
...fields...
<% topic_form.fields_for :articles do |article_form| %>
...fields...
Run Code Online (Sandbox Code Playgroud)
... Rails自动创建链接器,这很棒. 现在我的问题是:我的链接器模型还具有我希望能够通过"新主题"表单更改的属性.但是Rails自动创建的链接器除了topic_id和article_id之外,其所有属性都有nil值.如何将其他链接器属性的字段放入"新主题"表单中,这样它们就不会出现?
我知道用@public关键字定义公共实例变量是可能的.但是,Objective-C语法不允许访问其他类的变量.我应该从@public Ivar中得到什么功能?或者我如何访问其他类'Ivars?
这段代码:
#include <boost/signals.hpp>
#include <boost/bind.hpp>
#include <boost/mem_fn.hpp>
#include <iostream>
class Recorder : public ::boost::signals::trackable {
public:
void signalled() {
const void *me = this;
::std::cerr << "Recorder at " << me << " signalled!\n";
}
};
void signalled()
{
::std::cerr << "Signalled!\n";
}
int main(int argc, const char *argv[])
{
::boost::signal<void ()> sig;
sig.connect(&signalled);
{
Recorder r;
sig.connect(::boost::bind(&Recorder::signalled, &r, _1));
sig();
}
sig();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
正在生成这些编译器错误:
In file included from move_constructor.cpp:2:
/usr/include/boost/bind.hpp: In instantiation of ‘boost::_bi::result_traits<boost::_bi::unspecified, void (Recorder::*)()>’:
/usr/include/boost/bind/bind_template.hpp:15: …Run Code Online (Sandbox Code Playgroud) 在你阅读完整篇文章之前请不要回答这个问题!谢谢.
由于所有助手都存在于C#中(如lambda或自动属性),因此我无法通过引用传递属性.让我们说我想这样做:
foo(ref my_class.prop);
Run Code Online (Sandbox Code Playgroud)
我收到错误所以我写了:
{
var tmp = my_class.prop;
foo(tmp);
my_class.prop = tmp;
}
Run Code Online (Sandbox Code Playgroud)
现在它有效.但请注意两件事:
它是通用模板,我没有放任何类型,只有"var",所以它适用于我必须通过的所有类型和数量的属性
我必须一遍又一遍地做,没有任何好处 - 这是机械工作
现有的问题实际上杀死了Swap等有用的功能.交换通常为3行,但由于它需要2个引用,因此调用它需要5行.当然这是无稽之谈,每次我都想用它来手写"交换".但这表明C#阻止了可重复使用的代码,这很糟糕.
那么 - 如果编译器自动创建临时变量(就像我手工做的那样)会发生什么不好,调用函数,并将值分配回属性?这有危险吗?我没有看到它,所以我很好奇你觉得为什么这个问题的设计看起来现在看起来如此.
干杯,
编辑因为280Z28提供了很好的例子来打败自动包装ref属性的想法,我仍然认为用临时变量包装属性会很有用.也许是这样的:
Swap(inout my_class.prop1,inout my_class.prop2);
Run Code Online (Sandbox Code Playgroud)
否则没有真正的交换C#:-(
我有一个字符串,我必须从中删除以下字符:'\ r','\n'和'\ t'.我尝试了三种不同的方法来删除这些字符并对它们进行基准测试,以便我可以获得最快的解决方案.
以下是我运行1000000次的方法和执行时间:
如果我要删除1或2个字符,它应该是最快的解决方案.但随着我投入更多的焦炭,它开始需要更多的时间
str = str.Replace("\r", string.Empty).Replace("\n", string.Empty).Replace("\t", string.Empty);
Run Code Online (Sandbox Code Playgroud)
执行时间= 1695
对于1或2个char,这比String.Replace慢,但是对于3个char它表现出更好的性能.
string[] split = str.Split(new char[] { '\t', '\r', '\n' }, StringSplitOptions.None);
str = split.Aggregate<string>((str1, str2) => str1 + str2);
Run Code Online (Sandbox Code Playgroud)
执行时间= 1030
最慢的,即使是1个字符.也许我的正则表达不是最好的.
str = Regex.Replace(str, "[\r\n\t]", string.Empty, RegexOptions.Compiled);
Run Code Online (Sandbox Code Playgroud)
执行时间= 3500
这些是我提出的三种解决方案.有没有更好更快的解决方案,这里的任何人都知道,或者我可以在此代码中做任何改进?
我用于基准测试的字符串:
StringBuilder builder = new StringBuilder();
builder.AppendFormat("{0}\r\n{1}\t\t\t\r\n{2}\t\r\n{3}\r\n{4}\t\t\r\n{5}\r\n{6}\r\n{7}\r\n{8}\r\n{9}",
"SELECT ",
"[Extent1].[CustomerID] AS [CustomerID], ",
"[Extent1].[NameStyle] AS [NameStyle], ",
"[Extent1].[Title] AS [Title], ",
"[Extent1].[FirstName] AS [FirstName], ",
"[Extent1].[MiddleName] AS [MiddleName], ",
"[Extent1].[LastName] AS [LastName], ", …Run Code Online (Sandbox Code Playgroud) 我的源文件树是这样的:
/src
/pkg
/foo
foo.go
foo_test.go
Run Code Online (Sandbox Code Playgroud)
在foo.go里面:
package foo
func bar(n int) {
...
}
Run Code Online (Sandbox Code Playgroud)
在foo_test.go里面:
package foo
func testBar(t *testing.T) {
bar(10)
...
}
Run Code Online (Sandbox Code Playgroud)
我的问题是: