This question already has answers here:
What does ##variable mean in Ruby?
(5 answers)
Closed 9 years ago.
I just started learning Ruby and I have been unable to find a good explanation on what is the difference between ## and # in terms of class variables. If anyone can provide a basic intuitive example, that would be really great. Also are they interchangeable?
A variable prefixed with ## is a class variable and one prefixed with # is an instance variable. A great description can be found in this answer: https://stackoverflow.com/a/5890199/1181886
# before a variable name : instance variable (one per instance)
## before a variable name : static variable (one per class)
Related
This question already has answers here:
JMeter retrieve value of value
(2 answers)
Closed 1 year ago.
''
I want to pass parameter inside parameter.
For ex. ${var1${var2}} something like this.
I am taking this variables from Test plan's user defined variables.
''
Try this one: ${__V(var1${var2})}
Jmeter documentation
You can use the __V() function. It will combine the two variables.
You can refer this for more details.
https://www.blazemeter.com/blog/here%E2%80%99s-what-do-combine-multiple-jmeter-variables
This question already has answers here:
How to do "late" string interpolation in Ruby
(6 answers)
Closed 5 years ago.
I have a string within a variable that contains a placeholder. So quite literally I have this:
x = "something_\#{environment}"
I want to perform placeholder substitution of x at runtime with a value of environment that isn't available at the point x is defined. I ultimately want to end up with:
y = "something_test"
Is there any way to accomplish this in Ruby?
Edit 1: this isn't solved using eval which is dead. Hence the linked duplicate doesn't address my question.
Use templates with %:
template = "something_%s"
#=> "something_%s"
x = template % ["test"]
#=> "something_test"
This question already has answers here:
What are the restrictions for method names in Ruby?
(5 answers)
Closed 7 years ago.
On the Chef Style Guide page appears this Ruby expression:
antarctica_hint = hint?('antarctica')
What exactly does the ? after hint and before ('antarctica') mean? Is it just part of the method name? (i.e. the method is called 'hint?' not 'hint')
It is part of method name, and people typically (not always) use it for methods that return boolean value.
An example from Ruby is Class#respond_to?
This question already has an answer here:
naked asterisk as parameter in method definition: def f(*)
(1 answer)
Closed 8 years ago.
In the rails code I came across following method definition def initialize(*)
I understand what def foo(*a) means but can't figure out significance of omitting identifier name after *. How do you access any arguments passed to this method?
Here's my guess.
It works because of second line:
def initialize(*)
super
...
end
So the method receives arbitrary number of arguments and passes all of them to super(as you know, super without arguments means take all arguments from original method).
And then in this case the names for arguments are not required.
This question already has an answer here:
naked asterisk as parameter in method definition: def f(*)
(1 answer)
Closed 10 years ago.
I was poking through the Rails 3 ActiveRecord source code today and found a method where the entire parameter list was a single asterisk.
def save(*)
I couldn't find a good description of what this does (though I have some ideas based on what I know about splat arguments).
What does it do, and why would you use it?
It means it can have any number of arguments (including zero) and it discards all those arguments.