Questions tagged [initializer]

Initializers are called to create a new instance of a particular type. In its simplest form, an initializer is like an instance method with no parameters

684 questions
21
votes
6 answers

C# initialiser conditional assignment

In a c# initialiser, I want to not set a property if a condition is false. Something like this: ServerConnection serverConnection = new ServerConnection() { ServerInstance = server, LoginSecure = windowsAuthentication, if…
Pomber
  • 1,165
  • 2
  • 9
  • 13
21
votes
3 answers

How can I access the Rails logger from an initializer?

Following the advice from my previous question, I placed my background process in an initializer named scheduler.rb. However, I'm having a hard time getting the newly-scheduled processes to log to the Rails logs. Is there a simple way for me to…
abeger
  • 6,766
  • 7
  • 41
  • 58
20
votes
4 answers

Class methods which create new instances

Apart from the standard [[MyClass alloc] init] pattern, some objects are built from static methods like MyClass *obj = [MyClass classWithString:@"blabla"] According to widespread memory management guides (including Apple's), you're only responsible…
pistacchio
  • 56,889
  • 107
  • 278
  • 420
19
votes
2 answers

Rails initializer that runs *after* routes are loaded?

I want to set a class attribute when my Rails app starts up. It requires inspecting some routes, so the routes need to be loaded before my custom code runs. I am having trouble finding a reliable place to hook in. This works PERFECTLY in the "test"…
19
votes
3 answers

Rails - How to set global instance variables in initializers?

I was using the predictor gem. I initialized the recommender in initializers/predictor.rb: require 'course_recommender' recommender = CourseRecommender.new # Add records to the recommender. recommender.add_to_matrix!(:topics, "topic-1",…
Trantor Liu
  • 8,770
  • 8
  • 44
  • 64
19
votes
2 answers

Creating an initializer

In rails, when creating an initializer, is it as simple as adding a file to the config/initializer director or do I need to make changes elsewhere? I'm asking because I keep getting errors every time I create an initializer for gems...and not sure…
user749798
  • 5,210
  • 10
  • 51
  • 85
18
votes
4 answers

How to default-initialize a struct containing an array in Rust?

What is the recommended way to declare a struct that contains an array, and then create a zero-initialized instance? Here is the struct: #[derive(Default)] struct Histogram { sum: u32, bins: [u32; 256], } and the compiler…
Andrew Wagner
  • 22,677
  • 21
  • 86
  • 100
17
votes
2 answers

Is jumping over a variable initialization ill-formed or does it cause undefined behaviour?

Consider this code: void foo() { goto bar; int x = 0; bar: ; } GCC and Clang reject it, because the jump to bar: bypasses variable initialization. MSVC doesn't complain at all (except using x after bar: causes a warning). We can do a…
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
17
votes
3 answers

Incrementing a variable used twice in an initializer list - undefined behavior?

Edit: Not already answered - the linked question was about ordinary r-values, initializer lists are a separate, if related concept. Is this statement well-defined, or is using the prefix increment operator in an initializer list, on a variable that…
Ray Hamel
  • 1,289
  • 6
  • 16
17
votes
4 answers

Rails initializer for development and production

I have the following code in /config/initializers/chargify.rb Chargify.configure do |c| c.subdomain = 'example' c.api_key = '123xyz' end But I have different settings for development and production. So, how would I have a different set of…
Shpigford
  • 24,748
  • 58
  • 163
  • 252
17
votes
3 answers

Static Initialization in Go?

I'm currently working on the Go Lang tutorial, but ran into problem with one of the exercises: https://tour.golang.org/methods/23 The exercise has me implement a ROT13 cipher. I decided to implement the cipher using a map from a byte to its rotated…
jlhawn
  • 373
  • 1
  • 3
  • 8
16
votes
3 answers

Initializer vs Constructor

I have heard that the __init__ function in python is not a Constructor, It's an Initializer and actually the __new__ function is the Constructor and the difference is that the __init__ function is called after the creation of the object and the…
oridamari
  • 561
  • 7
  • 12
  • 24
16
votes
2 answers

How to define extern variable along with declaration?

Wiki says: The extern keyword means "declare without defining". In other words, it is a way to explicitly declare a variable, or to force a declaration without a definition. It is also possible to explicitly define a variable, i.e. to force a…
haccks
  • 104,019
  • 25
  • 176
  • 264
15
votes
1 answer

what is the default weight initializer for conv in pytorch?

The question How to initialize weights in PyTorch? shows how to initialize the weights in Pytorch. However, what is the default weight initializer for Convand Dense in Pytorch? What distribution does Pytorch use?
urningod
  • 191
  • 1
  • 9
15
votes
10 answers

Running C++ code outside of functions scope

(I know) In c++ I can declare variable out of scope and I can't run any code/statement, except for initializing global/static variables. IDEA Is it a good idea to use below tricky code in order to (for example) do some std::map manipulation ? Here…
Emadpres
  • 3,466
  • 2
  • 29
  • 44
1 2
3
45 46