Rails - Local Variables versus Instance Variables - ruby

while learning Rails, I keep hearing Local vs Instance but I can't find a definition of the two & the differences. And I'd like to avoid making assumptions.
What are the two and how are they different?
Thanks

The main difference between local and instance variable is that local variable is only available in controller, where as instance variable is available in corresponding views also. The controller and views do not share local variables.
Thanks, Anubhaw

The main differences between local and instance variables are as follows
local variable has its scope restriction i.e not available to another methods where as instance available to another
local and instance variable is also available in view
instance variable is separate for each object

Related

Global Variables in Laravel for Views, Controllers, Models etc

I know this question has been asked before, but I am struggling to find an answer that fits my needs, or is for the most up to date version of Laravel.
Basically, I have a load of variables that will be created using data from the Route and the database. For example, I need to use the Route to set a variable called "current_page" which extracts details from the "pages" table using the unique url column. ($current_page = Page::where('url', <url-taken-from-route>)->first();)
$current_page will need to contain several relationships. There are also other global variables such as $current_site (this is a kind of multi-site environment with one home page and nested microsites) etc.
I feel the configuration files are not the right place to create these variables as a) the value of the variables can be nested and complicated and b) they are not constants but data retrieved from a database.
View Composers obviously only work with views, and a Helper class just feels too generic considering we are dealing with pages, sites, and possibly other global variables.
I gather global variables are frowned upon, but these truly are global. The values will always be the same (albeit dynamic) on every occasion, and will be required by most views and controllers.
I have also thought about putting methods in my already existing Page and Site controllers so I can call Page::current(), but am not sure putting static methods in a mostly non-static controller is the right thing to do. Also, this would make a database call every time I called Page::current() rather than having the current page stored in memory somewhere.
For now, I have created a BaseController which all other controllers extend from. I have put these variables in the constructor and manually passed them to the view in each method. (I know that is messy and doesn't work for models).
So what is the best solution here? Where to declare a global variable that can be used anywhere, but doesn't feel hacky?
Thanks

How to Store Regular Expression Extractor reference name as User defined variable (Global variable to test plan)

In Regular Expression Extractor I have stored reference name as prasad.
If I give my reference name to further scripts ${prasad} where ever I want it is working fine.
But I want to store these reference name as user defined variables (Global variables).
I am using only one thread group.
I am doing performance automation scripts so I want to store that reference name as user defined variable.
I didn't not understand properly,(Below Answer)..Can any one help me with brief explanation please...
This is stated in JMeter's best practices or mailing list answer:
Variables are local to a thread; a variable set in one thread cannot
be read in another. This is by design. For variables that can be
determined before a test starts, see Parameterising Tests (above). If
the value is not known until the test starts, there are various
options:
Store the variable as a property - properties are global to the JMeter
instance
To store in JMeter's "global variable"/property add JSR223 Sampler
props.put("a", vars.get("a"));
Now you can see you "global" variable ${a}

Alternatives to global variables

I have a program that will grab several global settings from an API when first logged in. These values are then used extensively throughout the program. Currently I am storing them in global variables, but it does not seem very OOP.
What are the alternatives to global variables for storing extensively used settings? Use constants? Class variables? Where would I initialize the values through the API call, since this would only need to happen once? I have seen some examples that instantiate a class to get to the variables but that does not make much sense to me.
I would like to set the values on login and after this call the variables everywhere else with a simple expression like Global.myvalue or GLOBAL_MYVALUE
The Singleton Pattern might be handy for this:
https://ruby-doc.org/stdlib-2.1.0/libdoc/singleton/rdoc/Singleton.html
It's hard to give you a concise answer based on the information you provided, but I would avoid using global variables at all costs.
A good starting point would be to think of a class that could be a common ancestor to all the places where you use these variables and store them in that class. If your subclasses inherit from that class, these variables will automatically be available in their context.
Edit: like #seph posted, the singleton pattern seems to be a much better solution though

Exposing attributes in a defined type in puppet

I'd like to access attributes that are inside instances of defined types from other classes/instances.
This is very similar to a question asked on SO before - In Puppet, how can I access a variable/attribute inside a defined type?, however from what I understood the answer was specifically related to accessing parameters as opposed to arbitrary variables.
For example, given the following defined type:
define server (
$server_name = 'my_server'
){
$server_history = 'A long story'
}
I can successfully use getparam(...) to fetch server_name but I cannot do the same for server_history.
Also, if server was a class as opposed to to a defined type, accessing this variable is straightforward using something like server::serverhistory
Does anyone have any ideas on how to expose these variables? Or am I approaching this completely the wrong way?
Edit: For some higher level context on what I'm trying to do my server type gets instantiated by 3 other classes. A variable in the server type builds out some directory paths based on parameters provided to it by these classes (which naturally, are specific to those classes). There are some other classes that would like to use the directory path variable to place files there.
You ask
I'd like to access attributes that are inside instances of defined types from other classes/instances.
and you go on to clarify that you're after
arbitrary variables.
In fact, ordinary variables in the body of a defined type are not attributes of that type, nor of any instance thereof. They are not part of the accessible persistent state of instances of such types at all.
More generally, Puppet treats defined types just like native types in almost every observable way, but by the same token, it does not provide any features that serve to distinguish defined types as a special case. You are looking for such a feature, and it does not exist.
Since your design idea will not work, you'll need to think of an alternative. You say
my server type gets instantiated by 3 other classes. A variable in the server type builds out some directory paths based on parameters provided to it by these classes (which naturally, are specific to those classes). There are some other classes that would like to use the directory path variable to place files there.
Since the paths you're after are characteristic of specific classes, it makes sense for them to be accessible directly via those classes. It seems odd to me that you would even want to access them indirectly via resources declared by those classes.

In JMeter, where is the best place to define variable?

In JMeter, I have added a Config Element of User Defined Variables to state my variable data. However, I see I can do the same thing up in the main Test Plan element.
When should you define your variables within the Test Plan and when should you define them within the Config Element? What's the pro/con of each?
Thanks.
They are the same. Take a look at User Defined Variables description:
The User Defined Variables element lets you define an initial set of variables, just as in the Test Plan
But using separate config elements makes sense for 2 reasons:
Script organization. For example if you have many variables, instead of dumping them all in main Test Plan element, you could define several config elements with the name that suggests what kind of variables they store.
Ability to use variables defined in previous User Defined Variables elements. According to the same reference:
UDVs are processed in the order they appear in the Plan, from top to bottom. <...> the variables are not available for use until after the element has been processed, so you cannot reference variables that are defined in the same element. You can reference variables defined in earlier UDVs or on the Test Plan.
So having several elements provides you with an ability to use previously defined variables in the further ones. One of the use cases is, for instance, definition of some reusable function(s) in one User Defined Variables element, and using them in the following User Defined Variables elements.

Resources