基本上我有一个initializer classatRAILS_ROOT/config/initialiers/app_constant.rb来让一切变得容易控制。
class AppConstant
APIURL = 'http://path.to.api'
end
Run Code Online (Sandbox Code Playgroud)
在 中RAILS_ROOT/model/user.rb,我有设置:
class User < ActiveResource::Base
self.site = AppConstant::APIURL
end
Run Code Online (Sandbox Code Playgroud)
当运行时rails s,我收到以下错误
<class:User>: uninitialized constant User::AppConstant::APIURL
Run Code Online (Sandbox Code Playgroud)
我知道问题是因为 Rails 在创建类后运行初始化程序。有没有办法让一些初始化器在 Rails 设置它的类之前运行?
require "#{Rails.root}\conf\initializers\app_constant.rb"最后,通过添加application.rb在 Rails 加载模型之前加载的 来解决这个问题。
所有结构都有一个自动生成的memberwise initializer,您可以使用它来初始化新结构实例的成员属性...
问题1:默认初始化器有什么特别之处,为什么不能简单地称为默认初始化器?为什么要添加“成员”?是不是因为它列出了Structure 中定义的所有成员属性。并且在创建实例时,您还必须遵循 Structure 中定义的顺序。
问题 2是否还有其他特殊的初始化器有自己的特殊名称?如果是这样,它们看起来像什么。
[注释第一部分:] 与 Vadian 先生进一步讨论
[注第二部分:]
#include<iostream>
using namespace std;
long long int memo[20] = {-1}; //create memo table and initialise to -1
long long int fibo(long long int n)
{
if(memo[n]>-1) //changing this to if(memo[n]>0) works fine
return memo[n]; //but as such this gives 0 from all my inputs
if(n<=2)
return 1;
memo[n] = fibo(n-1) + fibo(n-2); //recurse
return memo[n];
}
int main()
{
long long int n;
cin>>n;
cout<<fibo(n)<<endl; //calls the fibo function
for(int i=0;i<20;i++) //this prints my memo table used...
cout<<memo[i]<<" ";
} …Run Code Online (Sandbox Code Playgroud) 我有以下课程:
public abstract class A()
{
public static final SomeString = null;
static()
{
SomeString = "aaa";
}
}
Run Code Online (Sandbox Code Playgroud)
何时调用此静态方法以及如何调用?
创建这样的静态方法(没有名称/返回类型)的目的是什么?
所以,我对C++编程相当新,但我已经将SDL广泛用于python和FreeBASIC.我确定我在这里遗漏了一些愚蠢的东西,但无论我尝试什么,我都会在我的video.h文件中收到错误"错误:在'命名空间'之前预期的初始化程序".这让我有点疯狂.
#include "SDL/SDL.h"
#include <iostream>
namespace video {
// This is here because like video, everything uses it and the players should never be able to touch it.
int rolldice(int minimumroll, int maximumroll, int numberofdice);
// Same Here.
char* charraystring(std::string prestring);
// Now we're in video proper
// This function loads an image, checks to make sure it works, returns the image, and unloads the testing surface.
SDL_Surface* loadimage(std::string path);
// This is an optimized blitter that will exit with …Run Code Online (Sandbox Code Playgroud) 我目前正在使用初始化程序将config.yml文件加载到AppConfig哈希中,该哈希提供对环境变量的访问.对于生产我使用在服务器上设置的环境变量.如果未设置环境变量(即在开发和测试中),我使用以下代码回退到config变量.
ENV['FACEBOOK_API_KEY'] || AppConfig['facebook_api_key']
Run Code Online (Sandbox Code Playgroud)
我的问题是我需要在环境特定的文件(development.rb/production.rb等)中提供这些变量,但是这个文件在初始化器之前加载.我应该怎么处理这个?
我正在尝试使用to_prepare事件来处理新的Rails 3.2.1项目.我放置了以下内容:
Rails.application.config.to_prepare do
puts 'here i am before a request'
end
Run Code Online (Sandbox Code Playgroud)
进入config/initializers下的初始化程序.根据此处的文档,当在开发模式下运行时,此块应该在应用程序的每个请求上运行,并且仅在生产中运行一次.我正在开发模式,这个块不会在每个请求上运行,而是仅在我启动应用程序时运行,而不是再次运行.
以下是我加载应用程序时的输出示例.
rails s
=> Booting WEBrick
=> Rails 3.2.1 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
here i am before a request
[2012-03-02 20:29:46] INFO WEBrick 1.3.1
[2012-03-02 20:29:46] INFO ruby 1.9.2 (2011-07-09) [x86_64-darwin11.2.0]
[2012-03-02 20:29:46] INFO WEBrick::HTTPServer#start: pid=37897 port=3000
Run Code Online (Sandbox Code Playgroud)
当我发出后续请求时,不显示字符串'here i is ...',只显示Rails日志的常规输出.我所有的搜索都只提到了文档,似乎告诉我这样做.有什么我可能会失踪的吗?
我正在使用Sorcery进行身份验证,我需要在其初始化程序中设置第三方身份验证.
初始化程序有一行如下所示:
config.twitter.callback_url "http://example.dev/auth/callback?provider=twitter"
Run Code Online (Sandbox Code Playgroud)
... example.dev当我在本地开发中使用Pow时,主机名在哪里.这必须是example.com如果应用程序是在生产,或者staging.example.com如果它在分期等.
我想将此行设置为如下所示:
config.twitter.callback_url "#{Rails.hostname}/auth/callback?provider=twitter"
Run Code Online (Sandbox Code Playgroud)
...但是request.host我知道的唯一方法就是知道这一点,它只能在控制器级别使用.
我可以使用条件测试并为每个环境手动设置主机名,但是当我在不同的本地和登台环境中进行测试时,能够以编程方式设置它是很棒的.
有什么建议?
通过JLS 8.3.2.3时,我无法理解以下代码.
class Z {
static { i = j + 2; }
static int i, j;
static { j = 4; }
}
Run Code Online (Sandbox Code Playgroud)
代码导致错误 Cannot reference a field before it is defined
但是,如果我将代码更改为
class Z {
static { i = 2; }
static int i, j;
static { j = 4; }
}
Run Code Online (Sandbox Code Playgroud)
代码正在编译中.但在这两种情况下,变量定义都在初始化块之后.这背后的秘密是什么?
此代码有效:
import UIKit
class wheel: UIControl {
}
Run Code Online (Sandbox Code Playgroud)
但是这段代码没有:
class wheel: UIControl {
override init(frame: CGRect) {
super.init(frame: frame)
}
Run Code Online (Sandbox Code Playgroud)
当我覆盖init(frame:CGRect)但它没有显示错误" 必须在子类中提供所需的初始化程序初始化UIControl " init(coder aDecoder: NSCoder).
为什么我要实施init(coder aDecoder: NSCoder)?如果我没有实现,为什么我不需要实现init(frame: CGRect)呢?
我发现了一个类似的Stack Overflow帖子,但它没有解释: Swift:错误:'required'initulator'init(coder :)'必须由'UIView'的子类提供
initializer ×10
c++ ×2
java ×2
ruby ×2
static ×2
swift ×2
arrays ×1
config ×1
environment ×1
hostname ×1
ios ×1
jls ×1
overriding ×1
sdl ×1