Is There a 'Variable_Get' Method? If Not, How to Create My Own

Is there a 'variable_get' method? If not, how can I create my own?

In Ruby 2.1 and later, you can use Binding#local_variable_get.

In prior versions of Ruby, you have to use eval. If you want to do some sanity-checking before evaluating a supposed variable name, you can check whether the named variable is in local_variables.

Use Variable Which is in main method in Another Method

import java.util.Scanner;
public class Alpha
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
int n;

System.out.println("Enter no. of stars");
n = input.nextInt();

Loop(n); //calls Loop function and passes parameter n
}
public static void Loop(int n) //this function now expects a number n
{
for (int counter = 1; counter <= n; counter++)
{
System.out.println("*");
}
}
}

If I assign a value to a variable inside a method, does it persist beyond the scope of that method?

Each method in a class will inherit any attribute (variable) or any method that directly belongs to that class. Say

public class Bicycle {

public int gear;
public int speed;

// the Bicycle class has
// two methods

public void setGear(int newValue) {
gear = newValue;
}

public void speedUp(int increment) {
speed += increment;
}
}

Let's get the setGear method

public void setGear(int newValue) {
gear = newValue;
}

As you can see, I can access the 'gear' attribute because it belongs to the Bicycle class. The parameter '(int newValue)' belongs to this method exclusively, meaning I can only access this variable inside this method, like this:

public void setGear(int newValue) {
newValue = gear;
//this wouldn't make much sense logic wise, but it is
//valid code since I can use newValue only inside the setGear method

speedUp(10);
//the method 'speedUp' belongs to the bicycle class, so I can use
//it inside another method belonging to the same class
}

Alternatively, I can use the this keyword to say that I am referring class attributes, like this:

public void setGear(int gear) {
this.gear = gear;
//I am telling the method that the class attribute 'gear' will
//receive my parameter value 'gear'
//this is useful when my parameter variable name has the same
//name as my class attribute
}

Edit: forgot to give credit to the oficial oracle docs https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html

Using variable from one method in another method

Aside: This is a java specific question, not an object-oriented question.

The crux of your question is about java scopes. The variable response is defined in the GetActivityById method. That method has a { and a }. That is the scope of the variable. What scope of a variable means, is that the variable is only visible - meaning, it only "exists" [1] after it was defined, [2] until the end of the scope (the curly brackets) it was defined in.

However, if you call a method, methodB from methodA, then methodB will have its own scope, which is separate from the scope of methodA which is the method calling it.

The method bodyResponse only has access to variables defined in its scope or passed into it as a parameter. So, to share a variable from one method to another, one way to do that is to pass it as a variable. You did this already with the exe variable. So, do the same for the response variable to fix this error:

public static void GetActivityById(){

Response response=Authentication.Creds("www.randomurl.com");

System.out.println("The extracted thing is: " + bodyResponse("name", response));//note I added *response*

}
public static String bodyResponse(String exe, Response response){//note I added *response*


JsonPath jsonPathEvaluator = response.jsonPath();
String bodyExample = jsonPathEvaluator.get(exe).toString();
return bodyExample;
}

Get value from string representing local variable

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


Related Topics



Leave a reply



Submit