instance-variables

Same instance variable for all actions of a controller

心已入冬 提交于 2019-11-28 19:42:37
I have a rails controller with two actions defined: index and show . I have an instance variable defined in index action. The code is something like below: def index @some_instance_variable = foo end def show # some code end How can I access the @some_instance_variable in show.html.erb template? Unless you're rendering show.html.erb from the index action, you'll need to set @some_instance_variable in the show action as well. When a controller action is invoked, it calls the matching method -- so the contents of your index method will not be called when using the show action. If you need @some

Python - should all member variables be initialized in __init__

久未见 提交于 2019-11-28 19:07:43
Maybe this is more of a style question than a technical one but I have a python class with several member variables and I want to have it work so that some of the member variables are initialized when the user first creates an instance of the class (i.e. in the __init__ function) and I want the other member variables to be defined from arguments of member functions that will be called later on. So my question is should I initialize all member variables in the __init__ function (and set the ones that will be defined later on to dummy values) or initialize some in the __init__ function and some

python: What happens when class attribute, instance attribute, and method all have the same name?

て烟熏妆下的殇ゞ 提交于 2019-11-28 18:41:29
How does python differentiate a class attribute, instance attribute, and method when the names are the same? class Exam(object): test = "class var" def __init__(self, n): self.test = n def test(self): print "method : ",self.test test_o = Exam("Fine") print dir(test_o) print Exam.test print test_o.test test_o.test() Output : ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'test'] <unbound

Access instance variable from outside the class

大憨熊 提交于 2019-11-28 16:28:02
If an instance variable belongs to a class, can I access the instance variable (e.g. @hello ) directly using the class instance? class Hello def method1 @hello = "pavan" end end h = Hello.new puts h.method1 knut Yes, you can use instance_variable_get like this: class Hello def method1 @hello = "pavan" end end h = Hello.new p h.instance_variable_get(:@hello) #nil p h.method1 #"pavan" - initialization of @hello p h.instance_variable_get(:@hello) #"pavan" If the variable is undefined (first call of instance_variable_get in my example) you get nil . As Andrew mention in his comment: You should not

Ruby Metaprogramming: dynamic instance variable names

断了今生、忘了曾经 提交于 2019-11-28 15:41:32
Let's say I have the following hash: { :foo => 'bar', :baz => 'qux' } How could I dynamically set the keys and values to become instance variables in an object... class Example def initialize( hash ) ... magic happens here... end end ... so that I end up with the following inside the model... @foo = 'bar' @baz = 'qux' ? Chuck The method you are looking for is instance_variable_set . So: hash.each { |name, value| instance_variable_set(name, value) } Or, more briefly, hash.each &method(:instance_variable_set) If your instance variable names are missing the "@" (as they are in the OP's example),

Private members in CoffeeScript?

二次信任 提交于 2019-11-28 15:33:40
Does somebody know how to make private, non-static members in CoffeeScript? Currently I'm doing this, which just uses a public variable starting with an underscore to clarify that it shouldn't be used outside of the class: class Thing extends EventEmitter constructor: (@_name) -> getName: -> @_name Putting the variable in the class makes it a static member, but how can I make it non-static? Is it even possible without getting "fancy"? Is it even possible without getting "fancy"? Sad to say, you'd have to be fancy . class Thing extends EventEmitter constructor: (name) -> @getName = -> name

Directly accessing an instance variable vs. Using an accessor method

那年仲夏 提交于 2019-11-28 15:30:21
Can anyone explain the difference between accessing an instance attribute via self.attribute and by @attribute ? sepp2k self.attribute calls the method attribute . self.attribute = value calls the method attribute= with the argument value . @attribute and @attribute = value get/set the value of the instance variable @attribute . So basically they're two entirely different things. However if you call attr_accessor :attribute it defines the method attribute to return @attribute and the method attribute=(value) to set @attribute = value . So in that case, there is no difference. "Accessing

Passing variables, creating instances, self, The mechanics and usage of classes: need explanation [closed]

社会主义新天地 提交于 2019-11-28 15:16:33
I've been sitting over this the whole day and Im a little tired already so please excuse me being brief. Im new to python. I just rewrote a working program, into a bunch of functions in a class and everything messed up. I dont know if it's me but I'm very surprised I couldn't find a beginner's tutorial on how to handle classes on the web so I have a few questions. First of all, in the __init__ section of the class I have declared a bunch of variables with self.variable=something . Is it correct that I should be able to access/modify these variables in every function of the class by using self

Ruby convert Object to Hash

浪尽此生 提交于 2019-11-28 15:12:03
Let's say I have a Gift object with @name = "book" & @price = 15.95 . What's the best way to convert that to the Hash {name: "book", price: 15.95} in Ruby, not Rails (although feel free to give the Rails answer too)? Vasiliy Ermolovich class Gift def initialize @name = "book" @price = 15.95 end end gift = Gift.new hash = {} gift.instance_variables.each {|var| hash[var.to_s.delete("@")] = gift.instance_variable_get(var) } p hash # => {"name"=>"book", "price"=>15.95} Alternatively with each_with_object : gift = Gift.new hash = gift.instance_variables.each_with_object({}) { |var, hash| hash[var

Error: identifier expected in Java

落爺英雄遲暮 提交于 2019-11-28 12:59:09
I am a new learner of Java. I learned some of the Java core concepts. I got the identifier expected error when run my following code: class Sekar { public static int i,j,k; i = 900; static void max() { j = 100; if(i>j) { k=i; } else { k=j; } System.out.println("The maxmimum vale between"+i+"and"+j+"is :"+k); } public static void main(String[] args) { max(); } } When I compile my code, I get the following error: error: identifier expected i = 900; ^ Can any one explain why this error happens here? When I google about identifier expected error, I found that this error happens when variables are