instance-variables

Rails: access controller instance variable in CoffeeScript or JavaScript asset file

心已入冬 提交于 2019-12-17 08:31:43
问题 In Rails 3.1 it is not possible to access controller instance variables in an asset js.erb or coffee.erb file using syntax such as <%= @foo %>, where @foo is set in the controller. So then the question is what are the best ways for passing controller variables to CoffeeScript or JavaScript assets. This question has kind of been asked in multiple convoluted forms on the forum, but my point in asking it again is to have a place where all recommendations are gathered together, and the code

Java Instance Variables vs Local Variables

我与影子孤独终老i 提交于 2019-12-17 06:35:10
问题 I'm in my first programming class in high school. We're doing our end of the first semester project. This project only involves one class, but many methods. My question is best practice with instance variables and local variables. It seems that it would be much easier for me to code using almost only instance variables. But I'm not sure if this is how I should be doing it or if I should be using local variables more (I would just have to have methods take in the values of local variables a

Java Instance Variables vs Local Variables

。_饼干妹妹 提交于 2019-12-17 06:35:04
问题 I'm in my first programming class in high school. We're doing our end of the first semester project. This project only involves one class, but many methods. My question is best practice with instance variables and local variables. It seems that it would be much easier for me to code using almost only instance variables. But I'm not sure if this is how I should be doing it or if I should be using local variables more (I would just have to have methods take in the values of local variables a

class variables is shared across all instances in python? [duplicate]

老子叫甜甜 提交于 2019-12-17 05:02:00
问题 This question already has answers here : How to avoid having class data shared among instances? (7 answers) Closed last year . I started coding in python a week ago, it is my mistake i started coding using oops,classes and objects that soon. I assumed my C++ proficiency will help.... I got bit by the following code class A: var=0 list=[] def __init__(self): pass Here to my surprise, var and list are kinda global variable, it is shared across all instances it seems.... What I thought was it

Instance variable: self vs @

喜你入骨 提交于 2019-12-17 04:14:19
问题 Here is some code: class Person def initialize(age) @age = age end def age @age end def age_difference_with(other_person) (self.age - other_person.age).abs end protected :age end What I want to know is the difference between using @age and self.age in age_difference_with method. 回答1: Writing @age directly accesses the instance variable @age . Writing self.age tells the object to send itself the message age , which will usually return the instance variable @age — but could do any number of

Where to initialize variables in Rails 3

一曲冷凌霜 提交于 2019-12-14 03:52:24
问题 I've been discovering Ruby on Rails during the last 3 months and I'm fascinated. But several things I don't know which is the best way to do them. Now I'm stack with the initialization of some variables. All the application is running over ajax, so all the instance variables should be initialize only once. I've tried writting a method 'initialize' in the applicationController which is run just once, but the problem is that I'm accesing the request object which is not initialized yet. I've

java - Should I use a global instance of java.util.Random, or construct one everytime it is used?

独自空忆成欢 提交于 2019-12-13 13:13:10
问题 The title pretty much sums it up. I have seen people construct an instance of Random globally, and use it in all of their code, and I have also seen people that construct an instance everytime they want to use Random. My question is: When, if ever, should I construct a new instance of Random for generating random numbers? Math.random() stores a Random instance in a RandomNumberGeneratorHolder, and calls it every time Math.random() is called. My view: I should use a global Random() instance,

C# Copying instance variable to local variable in functions of same class

被刻印的时光 ゝ 提交于 2019-12-13 13:03:41
问题 I have been looking through some code on an open source project recently and found many occurrences of this kind of code: class SomeClass { private int SomeNumber = 42; public ReturnValue UseSomeNumber(...) { int someNumberCopy = this.SomeNumber; if (someNumberCopy > ...) { // ... do some work with someNumberCopy } else { // ... do something else with someNumberCopy } } } Is there any real benefit to making a copy of the instance variable? 回答1: No unless you don't want to change the value of

Object with BigDecimals returns empty strings on to_s

倾然丶 夕夏残阳落幕 提交于 2019-12-13 06:06:45
问题 I have a location table I use to store geo coordinates: class Location < ActiveRecord::Base # Location has columns/attributes # BigDecimal latitude # BigDecimal longitude (...) def to_s @latitude.to_s << ', ' << @longitude.to_s end end However, when I call to_s on a Location, the BigDecimal inside converts to an empty string. ruby > l => #<Location id: 1, latitude: #<BigDecimal:b03edcc,'0.4713577E2',12(12)>, longitude: #<BigDecimal:b03ecb4,'-0.7412786E2',12(12)>, created_at: "2011-08-06 03:41

How to do OptionContext parsing on an instance?

Deadly 提交于 2019-12-13 01:05:31
问题 I'm trying to get options parsing using OptionContext to work. My code so far: public class Options : GLib.Object { public string option_output = ""; public Options () { } public void parse (string args[]) throws OptionError { // string option_output; const OptionEntry[] options = { { "output", 'o', 0, OptionArg.FILENAME, ref option_output, "file name for encoded output (required);", "FILE" }, {null} }; var opt_context = new OptionContext ("- vpng2theora"); opt_context.set_help_enabled (true)