how to pass a variable to *args function parameter - ruby

I have the following line of code:
str.start_with?("str1", "str2", "str3")
Is it possible I can pass a variable/list into this function instead? Something like:
list_of_strs = ["str1", "str2", "str3"]
str.start_with?(list_of_strs)

Use * to expand the array.
str.start_with?(*list_of_strs)

Related

Pass Variable inside the another variable in Jmeter

I Have a scenario like need to pass a random variable example ${__Random(1,25,)} in inside one more variable like
Globel variable:
Test = TestResults
${__Random(1,25,)}
Sample Request Data:
${Test(randome variable)}, ${Test(randome variable)},${Test(randome variable)},-------- ${Test(randome variable)}
Sample expected response:
TestResults1,TestResults10,TestResults5,----------- TestResults20
How should I pass this scenario?
Use the V function
${__V(TestResults${__Random(1,25,)})}
V (variable) function returns the result of evaluating a variable name expression. This can be used to evaluate nested variable references
You can replace TestResults with ${yourVarName} if you want to dynamically use additional variable

How do I evaluate a string key in Groovy so that it outputs a value?

we're defining a key/value property in ~/.gradle/gradle.properties like so:
~/.gradle/gradle.properties:
FOO=BAR
If we do:
println "$FOO" // prints BAR
it works, but if we try to make it a function that looks like this:
def getEnvValueForKey = { keyStr ->
return "$keyStr"
}
getEnvValueForKey("FOO") returns FOO instead of BAR
How do we make that work?
Your getEnvValueForKey(key) function returns always a GString representation of a variable passed as a parameter. The expression "$keyStr" is actually an alternative for "" + keyStr.toString(). The same thing happens when you call "$FOO" == "" + FOO.toString().
If you want to get a property defined in gradle.properties file you can redefine your function to something like this:
def getEnvValueForKey = { keyStr ->
return this.getProperties().getOrDefault(keyStr, null)
}
Calling getEnvValueForKey("FOO") in this case evaluates to
this.getProperties().getOrDefault("FOO", null)
If in current scope variable FOO exists it will return its value and null otherwise.
Keep in mind that this.getProperties() returns a map of all properties/variables defined in current scope of Gradle task being executed.

Rails dynamic params.require(...).permit(...) syntax?

I can do this code:
params.require(:something).permit(:param_a,:param_b)
And this:
params.require(:something).permit(:param_a,:param_c_attributes:[])
My problem is that I need to select the permit parameters depending if some parameter exists. So I tried:
premit_params = {:param_a,:param_c_attributes:[]}
premit_params = {:param_a,:param_d} if params[:something] && params[:something][:param_d]
params.require(:something).permit(premit_params)
But it's not working.
BTW: Using Rails 5.1
It doesn't work because permit doesn't expect a hash as an argument, but a list of parameters.
Collect your arguments in an array and split that array with the splat operator (*) to list or arguments:
premit_params = [:param_a, { :param_c_attributes: [] }]
premit_params = [:param_a, :param_d] if params.dig(:something, :param_d)
params.require(:something).permit(*premit_params)
You can check if the parameter you want exits
For Example:
if (user_params.has_key?(:name))
end
Moreover, parameters are saved in hash so you have different methods you can use to apply your logic
https://ruby-doc.org/core-1.9.3/Hash.html

How to access attribute which are stored as variables

model Sample has attributes car,bike
x ="bike"
y = Sample.new
How can I do?
y.x ??
It gives me an error
Is there any way I can do it, I know that x is an attribute but I dont know which one.
So how can I get y.x?
You can use send to invoke a method on an object when the method is stored as a string:
x = "bike"
y = Sample.new
y.send(x) # Equivalent to y.bike
The following are equivalent, except that you may send protected methods:
object.method_name
object.send("method_name")
object.send(:method_name)
You have to use dynamic message passing. Try this:
y.send :bike
Or, in your case
y.send x

Create a ruby Proc from a string

I want to define the block as a string, then create the lambda.
The following example does not work.
Is something like this possible?
code_string = "|x|x*2"
l = lambda {eval(code_string)}
l.call(3) => 6
This works
eval "lambda { " + code_string + " }"
I just don't know why this one does and the other does not.
eval "lambda {#{code_string}}"

Resources