刚刚看到一个有趣的可能性,在Scala中为高阶函数(如foreach或map)初始化代码块:
(1 to 3) map {
val t = 5
i => i * 5
}
(1 to 3) foreach {
val line = Console.readLine
i => println(line)
}
Run Code Online (Sandbox Code Playgroud)
这是一些记录的功能还是我应该避免这样的结构?我可以想象,"初始化"块进入构造函数,闭包本身变成了apply()方法?
感谢Pat的原始问题(http://extrabright.com/blog/2010/07/10/scala-question-regarding-readline)
我的webapp需要加密其会话数据.我设置的是:
config/initializers/encryptor.rb:
require 'openssl'
require 'myapp/encryptor'
MyApp::Encryptor.config[ :random_key ] = OpenSSL::Random.random_bytes( 128 )
Session.delete_all
app/models/session.rb:
require 'attr_encrypted'
class Session < ActiveRecord::Base
attr_accessible :session_id, :data
attr_encryptor :data, :key => proc { MyApp::Encryptor.config[ :random_key ] }, :marshal => true
# Rest of model stuff
end
Run Code Online (Sandbox Code Playgroud)
一切都很好,并保证会话数据安全.这是问题:当我运行自定义rake任务时,它会加载初始化程序并清除所有会话.不好!
我可以在初始化程序中放置什么来确保它仅用于webapp初始化?或者,我可以在初始化程序中添加什么来使其不用于rake任务?
更新:好的,我现在所做的就是添加MYAPP_IN_RAKE = true unless defined? MYAPP_IN_RAKE到我的.rake文件中.然后在我的初始化程序中我做:
unless defined?( MYAPP_IN_RAKE ) && MYAPP_IN_RAKE
# Web only initialization
end
Run Code Online (Sandbox Code Playgroud)
似乎工作.但我愿意接受其他建议.
每当有人访问该网站时,Rails应用程序中的初始化程序是否都会运行?
例如,如果我的服务器在德克萨斯州上午10点启动,并且有人在下午1点从纽约访问我的网站,并且有人在晚上10点从洛杉矶访问,那么当来自纽约的人们在轨道应用程序中运行初始化器时和洛杉矶访问,或者只在我在德克萨斯州启动服务器时才运行初始化程序?
我问的原因是因为我在初始化文件中使用case表达式来根据访问应用程序的时间来更改电子邮件设置.只有在有人访问该网站时才会运行初始化程序,这才有意义.如果它们仅在服务器启动时运行,那么它只会是一个案例......
如果这不是正确的地方,或者如果初始化程序仅在德克萨斯州启动服务器时运行(例如),那么您将在何处放置此代码?
case
when Time.now.hour == 0
ActionMailer::Base.smtp_settings = {
:user_name => "blahblah@example.com",
:password => "blahblah",
:address => "smtp.examplel.com",
:port => 25,
:tls => true
}
when Time.now.hour == 1
ActionMailer::Base.smtp_settings = {
:user_name => "blah@example.com",
:password => "eatshit",
:address => "smtp.example.com",
:port => 25,
:tls => true
}
end
Run Code Online (Sandbox Code Playgroud) 我试图在Rails 3中构建一个gem并在其中我试图传递一个初始化器:
Credentials.configure do |config|
file = File.read("#{Rails.root}/config/twitter.yaml")
file_config = YAML.load(file)
config.consumer_key = file_config[Rails.env][:consumer_key]
config.consumer_secret = file_config[Rails.env][:consumer_secret]
config.callback_url = URI.escape(file_config[Rails.env][:callback_url])
config.time_stamp = Time.now.to_i
end
Run Code Online (Sandbox Code Playgroud)
然后我试着像这样称呼它:
Credentials.time_stamp
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
uninitialized constant Twitter::Credentials
Run Code Online (Sandbox Code Playgroud)
问题是什么?
谢谢
我有以下代码的问题.正如我们所看到的,我已经处理了A的构造函数在C的构造函数中抛出的异常,为什么我还要在main函数中再次捕获并处理异常呢?
#include <iostream>
class WException : public std::exception
{
public:
WException( const char* info ) : std::exception(info){}
};
class A
{
public:
A( int a ) : a(a)
{
std::cout << "A's constructor run." << std::endl;
throw WException("A constructor throw exception.");
}
private:
int a;
};
class B
{
public:
B( int b ) : b(b)
{
std::cout << "B's constructor body run." << std::endl;
throw WException("B constructor throw exception");
}
private:
int b;
};
class C : public A, …Run Code Online (Sandbox Code Playgroud) 在Swift编程语言指南中,它说:
"默认初始值设定项的访问级别与初始化的类型相同."
摘录自:Apple Inc."The Swift Programming Language."iBooks. https://itun.es/us/jEUH0.l
然后它说:
"对于定义为public的类型,默认初始值设定项被视为内部.如果希望在另一个模块中使用无参数初始化程序时可以初始化公共类型,则必须自己提供一个公共无参数初始化程序作为类型定义的一部分."
摘录自:Apple Inc."The Swift Programming Language."iBooks. https://itun.es/us/jEUH0.l
是不是第二个与第一个相矛盾的陈述?
在工作中,我正在尝试一些,以反映我们的代码库.基本上我想要实现的是捕获数据成员的初始化器类型中的数据成员的指针:
template<class Class, int Class::*dataMember>
struct Reflect
{
operator int() {return 0;}
};
class Foo
{
public:
int bar = Reflect<Foo, &Foo::bar>{};
};
Run Code Online (Sandbox Code Playgroud)
虽然clang 3.4.1(http://gcc.godbolt.org/)和Intel C++ XE 14.0能够编译这段代码,但在使用MSVC12时,我收到以下错误消息:
错误C2065:'bar':未声明的标识符
错误C2975:'dataMember':'Reflect'的模板参数无效,是预期的编译时常量表达式
此外,gcc 4.9.2似乎也有问题:http://ideone.com/ZUVOMO.
所以我的问题是:
几周前我一直是Objective-C的开发人员,并且听说过Realm.另一方面,我一直希望一点一点地迁移到Swift,所以我创建了一个涉及Realm + Swift的小项目.
这是什么意思?我是Swift + Realm的新手.
无论如何,我为我想到的项目创建了一个小型演示/概念证明,我认为它必须更容易.但是Xcode编译器说不然.
我的问题是我的一个对象的初始化程序.
我的意图很简单,但显然Realm需要的初始化程度比我想要的要多.
我的一个Realm对象的代码是这样的:
import Foundation
import Realm
import RealmSwift
class Partida: Object
{
dynamic var id_partida: String
dynamic var inventario: Inventory?
required init ()
{
inventario = Inventory()
id_partida = "id_partida_test"
super.init()
}
required init(value: AnyObject, schema: RLMSchema) {
//fatalError("init(value:schema:) has not been implemented")
super.init(value: value, schema: schema)
}
required init(realm: RLMRealm, schema: RLMObjectSchema) {
//fatalError("init(realm:schema:) has not been implemented")
super.init(realm: realm, schema: schema)
}
override class func primaryKey() -> String? {
return …Run Code Online (Sandbox Code Playgroud) 基本上,为什么这是有效的:
auto p1 = new int[10]{5};
但这不是:
auto p1 = new int[10](5);
更一般地说,new-expression初始化程序的规则是什么?
我找到了以下内容:
- 如果省略new-initializer,则默认初始化对象(8.5).[注意:如果未执行初始化,则对象具有不确定的值. - 结束注释] - 否则,根据8.5的初始化规则解释new-initializer以进行直接初始化.
那么第二种情况是无效的,因为smth就像T((5))是无效的(从表达式直接初始化(5))?或者是什么原因?
编辑:好吧,我对(())事物的建议似乎很愚蠢,因为我认为没有理由为什么只应用于数组new-expression.
你能解释一下下面的对象实例化吗?怎么称呼?
我在哪里可以找到有关这种对象实例化的更多信息?
#include <string>
#include <iostream>
using namespace std;
class Car {
public:
string name;
int wheels;
};
int main() {
Car c{
name: "vw",
wheels: 4
};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用带有以下选项的 GCC:
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.4.0-1ubuntu1~18.04.1' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 …Run Code Online (Sandbox Code Playgroud)