Ruby - evaluate string placeholder in variable at runtime [duplicate] - ruby

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"

Related

Ruby lexical scope inside iterator block [duplicate]

This question already has answers here:
Why can I refer to a variable outside of an if/unless/case statement that never ran?
(3 answers)
Closed 5 years ago.
In Ruby (v2.5.0)...
[1,2,3].map do |i|
if i.eql?(3)
a = 123
end
defined?(a)
end
=> ["local-variable", "local-variable", "local-variable"]
Can someone please explain to me how a can be a local-variable (equal to nil) in the first and second iteration, if it's not set until the third iteration?
Thanks in advance!
I will answer quoting a book by A.Black: Well Grounded Rubyist, Chapter 6, p. 158. (second edition 2014):
When the Ruby parser sees the sequence identifier, equal-sign, and value, as in this expression,
a = 123
it allocates space for a local variable a. The creation of the variable - not the assignment of a value to it, but the internal creation of a variable - always takes place as a result of this kind of expression, event if the code isn't executed.

How does precedence apply on an expression with an assignment and a conditional? [duplicate]

This question already has an answer here:
How is a local variable created even when IF condition evaluates to false in Ruby? [duplicate]
(1 answer)
Closed 6 years ago.
I have the following simple snippet:
var = 1 if false
I would expect this to evaluate as:
(var = 1) if false
so var would be undefined. However, var gets defined and receives a nil as its value.
What am I missing here?
Ruby recognizes local variables during parsing. So, in your case, even thouogh it's not set to 1 (because the precedence of this expression is like you wrote), ruby knows that it's local variable and doesn't raise NameError.
Ruby parser defines var when it sees it on the lefthand side of an expression (even though its inside of a conditional that doesn’t run). So nil looks an appropriate value.

What does ? do in this Ruby expression? [duplicate]

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?

Assigning an undefined variable in a one line condition [duplicate]

This question already has answers here:
Ruby if vs end of the line if behave differently?
(5 answers)
Closed 8 years ago.
In Ruby, why can you write :
# b is not defined yet.
#
if b = true
a = b
end
# => a = true
But not a one-liner :
a = b if b = true
# => NameError: undefined local variable or method `b' for main:Object
Because the Ruby interpreter "creates" a local variable when it sees an assignment.
In the second case, it hasn't yet seen the assignment, so the variable doesn't exist when the expression is parsed.
To be more precise, a method is first parsed into an internal representation, and then, perhaps, the code will eventually be called and actually executed.
Variables are "created" in that parsing pass. It's really more a matter of declaration, it just means that the interpreter becomes aware of them. They won't be created in the sense of being given space or a value until the surrounding method is called by someone.

What does a single splat/asterisk in a Ruby argument list mean? [duplicate]

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.

Resources