variable-assignment

assignin('caller',…) within a function in Matlab

大兔子大兔子 提交于 2019-12-08 02:43:13
问题 I created this very useful bit of code to assign variables dynamically from a struct : function getParam(param) % this function extracts the fields of structure param and assigns them % to variables of corresponding names in the caller workspace allFieldsParam = fieldnames(param); for iField = 1:length(allFieldsParam) assignin('caller',allFieldsParam{iField},param.(allFieldsParam{iField})); end The problem is that when I call getParam within a function, sometimes it works and sometimes it

In Makefile assign path variable dependent if path exists

余生颓废 提交于 2019-12-08 02:39:41
问题 I develop my c++ app alternately on debian and ubuntu and the library root dir of informix database is different on both distributions. What's a nice way of handling it in Makefile so i don't have to change it manually each time? I thought of just testing on existance of the directory so it's more general then checking uname or lsb-release or hostname. And how is the syntax for assigning in a condition? I get the "missing seperator" error on try#2 // prepare INFORMIXDIR_DEB=/usr/informix

Mass assignment on construction from within ruby [duplicate]

泪湿孤枕 提交于 2019-12-08 00:57:45
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Idiomatic object creation in ruby Sometimes it's useful to assign numerous of a constructed arguments to instance variables on construction. Other than the obvious method: def initialize(arg1, arg2, arg3) @arg1, @arg2, @arg3 = arg1, arg2, arg3 end Is there a more concise idiom for achieving the same result? Something like that found in scala for instance: class FancyGreeter(greeting: String) { def greet() =

What is best practice in Ruby to avoid misusing assignment “=”?

人盡茶涼 提交于 2019-12-07 18:14:38
问题 I've been bitten a couple of times by forgetting that x = y in Ruby makes x refer to the same object as y; I'm too used to languages where it means, in Ruby terms, x = y.dup . Forgetting this, I inadvertently change y when I think it's safe on the right side of the assignment. I can see that it would make sense to avoid simple x = y assignments without a special reason, but the same thing can be lurking in other places such as name = (person.last_name.blank? ? 'unknown' : person.last_name)

Why does ruby define variables even if it never executes the variable assignment code?

混江龙づ霸主 提交于 2019-12-07 17:44:04
问题 Given the following code: a = true # let's assign `a` a value # and let's test if calling `b`, an unassigned variable, throws an error begin puts "The value of b is: #{b.inspect}" rescue NameError => e puts "Caught an error: #{e}" end a || b = true # the assignment should never be executed because `a` is `true` puts "The value of b is: #{b.inspect}" # will calling `b` still raise an error? We get the following result: Caught an error: undefined local variable or method `b' for main:Object The

What's the difference between Initialization and Assignment in C# [duplicate]

荒凉一梦 提交于 2019-12-07 09:25:17
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: C# variable initializations vs assignment Just like in the title, could someone please explain what is the difference between Initialization and Assignment in C#? I'm preparing for a test and I wanted to know what's the best way to answer this type of question. Thanks Cheers, n1te 回答1: When you initialize a variable you're declaring it into existence. PlasticCup mySippyCup = new PlasticCup(); When you assign,

In Java, is an expression assignable to a declared variable iff. it can be passed as a parameter declared with the same type?

て烟熏妆下的殇ゞ 提交于 2019-12-07 08:58:25
问题 This question was inspired by Java 8: Is it possible to assign a method reference to a variable?. As I currently understand (which may not be completely right), the invocation of, say fooMethod(FooType ft) as fooMethod(myFooInstance) causes an implicit assignment of myFooInstance to a local variable within fooMethod 's body, whose name is ft , and whose type is declared as FooType . Clearly, in this case, I could also assign myFooInstance to a field or local variable whose type is FooType ,

replace() vs “[<-”?

邮差的信 提交于 2019-12-07 08:47:40
问题 I recently stumbled across replace() and "[<-" . They seem to have similar functionality, for example with "[<-" I can do something like this: > x.tst <- array(1:6, c(2,3)) > s.tst <- array(0, c(2,3)) > s.tst [,1] [,2] [,3] [1,] 0 0 0 [2,] 0 0 0 > s.tst[1:3] <- 1 > "[<-"(x.tst, s.tst==1, 0) [,1] [,2] [,3] [1,] 0 0 5 [2,] 0 4 6 > x.tst [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 Can somebody help to clarify the difference? What are the strengths of replace vs "[<-" and vis versa? 回答1: They're

Unneeded parts when unwrapping tuple/list

有些话、适合烂在心里 提交于 2019-12-07 07:05:11
问题 Python is all about writing beautiful code. So, I was running pylint to check the "beautifulness" of my code, when I bump into something: Unused variable 'myvar1' From this part of my code: for myvar1, myvar2 in mylist: # Do stuff just using myvar2 mylist is a list of tuples, so I'm unwrapping the tuples into two variables ( myvar1 and myvar2 ). I'm defining those two variables just to unwrap the second one, because I don't need the other. So, here's my question: Is there a way to tell the

set default value in class constructor C#

戏子无情 提交于 2019-12-07 06:10:51
问题 I need a default value set and many different pages access and update..initially can I set the default value in the class constructor like this? What is the proper way to do this in C# .NET? public class ProfitVals { private static double _hiprofit; public static Double HiProfit { get { return _hiprofit; } set { _hiprofit = value; } } // assign default value HiProfit = 0.09; } 回答1: You can put it in the declaration: private static double _hiprofit = 0.09; Or if it's a more complicated