local-variables

Exit code of command substitution in bash local variable assignment [duplicate]

前提是你 提交于 2019-12-04 15:19:03
问题 This question already has answers here : Why does “local” sweep the return code of a command? (2 answers) Closed 3 years ago . How can I check the exit code of a command substitution in bash if the assignment is to a local variable in a function? Please see the following examples. The second one is where I want to check the exit code. Does someone have a good work-around or correct solution for this? $ function testing { test="$(return 1)"; echo $?; }; testing 1 $ function testing { local

Parameter scope vs local variable scope?

我是研究僧i 提交于 2019-12-04 13:30:11
I was reading my AP cs book and it talked about three types of variables: •Instance variables •Local variables •Parameters Instance variables are visible throughout the class etc... Parameters are only usable within the method and so are local variables . . . Therefore my question is why would they classify Parameters and Local variables as different categories of variables if they contain the same scope. . . Despite the different uses of them. Because they don't necessarily have the same scope. Take this case: // this is garbage code public void doSomething(int foo) { int meh = 0; while (true

NameError: undefined - have parsing rules for local variables changed in Ruby 2.1.2?

元气小坏坏 提交于 2019-12-04 03:20:30
问题 I am getting NameError: undefined local variable or method with ruby 2.1.2 As observed in this question, expressions like: bar if bar = true raises an undefined local variable error (provided that bar is not defined prior) because bar is read by the parser before it is assigned. And I believe that there used to be no difference with that with this expression: bar if bar = false The difference between the two is whether the main body is evaluated or not, but that should not matter if

Why is it not possible to get local variable names using Reflection?

こ雲淡風輕ζ 提交于 2019-12-04 02:57:43
If I have a code like this: public class Program { public static void Main() { string bar = ""; int foo = 24; } } I can get the local variables declared in Main using: var flag = BindingFlags.Static | BindingFlags.Public; var fields = typeof(Program).GetMethod("Main", flags).GetMethodBody().LocalVariables; This returns a IList<LocalVariableInfo> and the LocalVariableInfo has only three properties: IsPinned , LocalIndex and LocalType .So there is no Name property exists. What I'm wondering is that you can see the variable names in the generated IL code : .method public hidebysig static void

Local variables on stack

ε祈祈猫儿з 提交于 2019-12-03 21:57:22
To understand the stack frame concept, I wrote a little program for my own. First I will show you the code, a little sketch about it and then I will present my question: So, the program: int check_pw(char *password){ int valid = 0; char buffer[10]; strcpy(buffer, password); if(strcmp(buffer, "a") == 0){ valid = 1; } return valid; } int main(int argc, char *argv[]){ if(check_pw(argv[1])){ printf("OK\n"); } else{ printf("Wrong password\n"); } } I give the password as a command-line argument. And if it is equal to 'a', then it is ok. So, I think it is clear. Now the sketch how the stack frame of

ActionMailer pass local variables to the erb template

℡╲_俬逩灬. 提交于 2019-12-03 15:57:50
问题 I know I could define instance variables e.g: def user_register(username, email) @username = username @email = email mail(:to => email, :subject => "Welcome!", :template_name => "reg_#{I18n.locale}") end But, is there a way to use local variables instead, just like passing :locals to partials? 回答1: All options available in the mail method can be found at http://api.rubyonrails.org/classes/ActionMailer/Base.html#method-i-mail. We know that render has a :locals option. However we can see that

What is the difference between a threadvar and a local variable

假如想象 提交于 2019-12-03 12:32:36
问题 In my threads, I always declare local variables "normally", thus: procedure TMyThread.Execute ; var i : integer ; begin i := 2 ; etc, If I declare them thus: procedure TMyThread.Execute ; threadvar j : integer ; begin j := 2 ; how does execution/code generation/speed/thread-safety alter? 回答1: Well for a start the code with the threadvar is invalid syntax. A threadvar needs to have unit scope rather than local scope. Local variable Each invocation (including from different threads, and re

Get value from string representing local variable [duplicate]

邮差的信 提交于 2019-12-03 12:25:31
This question already has answers here : Is there a 'variable_get' method? If not, how can I create my own? (2 answers) I have a local variable name as a string, and need to get its value. variable = 22 "variable".to_variable? How can I get the value 22 form the string? shivam You can use eval . variable = 22 eval("variable") # => 22 However eval can be nasty . If you dont mind declaring an instance variable, you can do something like this too: @variable = 22 str = "variable" instance_variable_get("@#{str}") # => 22 binding.local_variable_get("variable") # => 22 use eval() method: variable =

Lambda assigning local variables

て烟熏妆下的殇ゞ 提交于 2019-12-03 11:50:34
Consider the following source: static void Main(string[] args) { bool test; Action lambda = () => { test = true; }; lambda(); if (test) Console.WriteLine("Ok."); } It should compile, right? Well, it doesn't. My question is: according to C# standard, should this code compile or is this a compiler bug? The error message: Use of unassigned local variable 'test' Note: I know , how to fix the error and i partially know , why does it happen. However, the local variable is assigned unconditionally and I guess, that compiler should notice that, but it does not. I wonder, why. Comment for answers: C#

Can static local variables cut down on memory allocation time?

≯℡__Kan透↙ 提交于 2019-12-03 10:43:48
Suppose I have a function in a single threaded program that looks like this void f(some arguments){ char buffer[32]; some operations on buffer; } and f appears inside some loop that gets called often, so I'd like to make it as fast as possible. It looks to me like the buffer needs to get allocated every time f is called, but if I declare it to be static, this wouldn't happen. Is that correct reasoning? Is that a free speed up? And just because of that fact (that it's an easy speed up), does an optimizing compiler already do something like this for me? For implementations that use a stack for