Retrieve original string prior to interpolation - ruby

I want to write a small patch to String class for my test environment that will warn for usage of double quotes without interpolation (#{foo}).
Is there a way to resolve if a string has derived from interpolation?
If yes, is there a way to retrieve the original un-interpolated value?

There is no "original string prior to interpolation"; a string is created after interpolation has been done. What you are trying to do is impossible at the Ruby level. The only way to do it is to parse the source, as LolWalid suggests, or hack the Ruby code at the C level.

Related

append or prepend to terraform variable v12

I read that v12 no longer requires interpolation syntax and I'd prefer to not use it whenever possible. One question I have is how do you prepend or append a string to a variable, such as for naming conventions?
with interpolation, you could
name = "${var.variable_name}-vnet"
new v12 syntax supposed to allow variables written like this, but i can't just add on a -vnet as above
name = var.variable_name
The intent of the change in Terraform 0.12 was to avoid the need for redundant interpolation syntax and in particular to avoid the confusion implied by "interpolating" something that isn't a string in order to assign a variable's value to an argument, not to remove the interpolation syntax altogether. If your goal is to concatenate two strings together then interpolation syntax is still likely to be the most concise way to do it, because string concatenation is what the interpolation syntax is for.
There are some other alternatives via built-in functions but it's debatable whether each of them are more or less readable than the interpolation approach:
name = join("-", [var.variable_name, "vnet"])
name = format("%s-vnet", var.variable_name)

How exactly do quotes in Ruby work to form a string?

I don't quite understand how string quotes in Ruby actually work. How does wrapping something in a quote suddenly make it a string? What exactly are the quotes doing? I'm trying to understand the C or core language implementation of this.
What exactly are the quotes doing?
The quotes themselves do nothing. They're just markers. Here's where a string starts, here's where it ends. When your code is being parsed to be executed, the parser will take what's between the quotes and make a string from that content. Simple as that.
If you take a compilers course in the school, chances are that you'll have to implement your own parser and compiler/interpreter for some toy language. Likely, with strings too. It's a fun exercise! :)
BTW, in ruby you can write a string literal in many ways. Not only using quotes. This is a string too, for example
html = <<-HTML
<head><title>stack overflow</title></head>
HTML
html # => " <head><title>stack overflow</title></head>\n"
In ruby the most common syntax for creating a string is using quotes like below.
my_msg = "Hello"
This is same in most other languages as well (c, java etc). AFAIK the language's parser is responsible for detecting the above syntax and continue to store Hello as a string in my_msg variable.
Ruby also has many other syntax for creating strings.

How to turn a string into a variable? (TI-84)

I'm trying to turn a string into a variable on my TI-84.
For example,
"XYZ"→Str0
fnInt(X²,sub(Str0,1,1),0,1)→A
But it's not letting me.
I know this seems like a really inefficient way of doing it (why not just do fnInt(X²,X,0,1)?), but in my program this would be very, very useful.
So is there a way to turn a string into a variable?
I don't think you can use a string where fnInt( expects a variable. The only thing I can recommend is to use a bunch of if statements, using I as the index of the variable you're using from your string. Basically, you'd have to write out these cases explicitly, since there's no way to make a string get interpreted as a variable for that function.
If I=1:fnInt(X²,X,0,1)→A
If I=2:fnInt(X²,Y,0,1)→A
If I=3:fnInt(X²,Z,0,1)→A
If you have a string as some function and want to evaluate it for some values, you can always store it to Y1, set the variables in it to what you want, and then just use Y1 as your evaluated function.

What is the simplest way to get UTF-8 substring in Julia

UTF-8 string in Julia cannot use slice operator because it slice the byte index of string not character. For example
s = "ポケットモンスター"
s[1:4]
s[1:4] will be "ポケ" not "ポケット".
I would like to know the simplest and most readable for get UTF-8 sub-string in Julia.
Perhaps this question calls attention to some missing functions in the standard string library (which is supposed to undergo changes in the next version of Julia). In the meantime, if we define:
substr(s,i,j) = s[chr2ind(s,i):chr2ind(s,j)]
Then,
substr(s,1,4)
Would be "ポケット"
You might want to consider using UTF32String instead of UTF8String, if you are going to be doing this a lot, and only converting to UTF8String if necessary, when you are finished.

Evaluating a frozen string

My vague understanding is that, with Ruby 2.2's frozen method on string or Ruby 2.3's frozen-string-literal: true pragma, a relevant frozen string literal is evaluated only once throughout program execution if and only if the string does not have interpolation. The following seems to illustrate this:
Not interpolated
#frozen-string-literal: true
5.times{p "".object_id}
Outputs (same object IDs):
70108065381260
70108065381260
70108065381260
70108065381260
70108065381260
Interpolated
#frozen-string-literal: true
5.times{p "#{}".object_id}
Outputs (different object IDs):
70108066220720
70108066220600
70108066220420
70108066220300
70108066220180
What is this property (i.e., being evaluated only once) called? It should be distinct from immutability.
Is my understanding of the condition when strings come to have such property correct? Where is the official documentation mentioning this?
Is there a way to make an interpolated string be evaluated only once?
Interning. The strings are said to be interned.
Not completely. It is more like if the interpreter can decide what the value of the string would be before evaluating it. For example, consider:
5.times { puts "#{'foo'}".object_id }
The id is the same even though there is interpolation involved.
No. This is an internal optimization. The main point of Object#freeze is immutability.
UPDATE: Only literal strings get internalized. This is evident here.
I couldn't find the part of the code responsible for interpolation. So I'm not sure why "#{'foo'}" is considered a literal string. Note that wherever this translation occurs, it is on a lower parser level and happens way before any actual processing. This is evident by the fact that String#freeze is mapped to rb_str_freeze, which doesn't call opt_str_freeze.
"Frozen" is not about whether the string is evaluated more than once. It is, you are right, about mutability.
A string literal will be evaluated every time the line containing it is encountered.
The (only) way to make it be evaluated only once, is to put it in a line of source code that is only executed once, instead of in a loop. A string literal in a loop (or any other part of source code) will always be evaluated every time that line of source code is executed in program flow.
This is indeed a separate thing than whether it is frozen/immutable or not, once evaluated.
The accepted answer is kind of misleading. "It is more like if the interpreter can decide what the value of the string would be before evaluating it." Nope. Not at all. It needs to be evaluated. If the string is frozen, then once it IS evaluated, it will use the same location in memory and the same object/object_id (which are two ways of saying the same thing) as all other equivalent strings. But it's still being evaluated, with or without interpolation.
(Without interpolation, 'evaluation' of a string literal is very very quick. With simple interpolation it's usually pretty quick too. You can of course use interpolation to call out to an expensive method though, hypothetically).
Without interpolation, I wouldn't worry about it at all. With interpolation, if you think your interpolation is expensive enough you don't want to do it in a loop -- the only way to avoid it is not to do it in a loop, but create the string once outside the loop.
Ruby docs probably talk about "String literals" rather than "literal Strings". A "String literal" is any String created by bytes in source code (using '', "", %Q[], or any of the other ways of creating strings literals in source code in ruby). With or without interpolation.
So what kinds of Strings aren't created by String literals? Well, a string created by reading in bytes from a file or network for instance. Or a String created by taking an existing string and calling a method on it that returns a copy, like some_string.dup. "String literal" means a string created literally in source code, rather than by reading from external input. http://ruby-doc.org/core-2.1.1/doc/syntax/literals_rdoc.html

Resources