Could someone explain why doing this:
%{#$"}
in irb produces the following?
=> "[\"enumerator.so\", \"enc/encdb.so\", \"enc/big5.so\", \"enc/cp949.so\", \"enc/emacs_mule.so\", \"enc/euc_jp.so\", \"enc/euc_kr.so\", \"enc/euc_tw.so\", \"enc/gb2312.so\", \"enc/gb18030.so\", \"enc/gbk.so\", \"enc/iso_8859_1.so\" ... ]
Thanks!
String Interpolation, it is all about combining strings together, but not by using the + operator. String Interpolation works only when we use double quotes (“”) for the string formation. String Interpolation provides an easy way to process String literals.
In Ruby, string interpolation refers to the ability of double-quoted strings to execute Ruby code and replace portions of that strings (denoted by #{ ... }) with the evaluation of that Ruby code.
concat is a String class method in Ruby which is used to Concatenates two objects of String. If the given object is an Integer, then it is considered a codepoint and converted to a character before concatenation.
Instead of terminating the string and using the + operator, you enclose the variable with the #{} syntax. This syntax tells Ruby to evaluate the expression and inject it into the string.
Ruby | String Interpolation Last Updated : 24 Sep, 2019 String Interpolation, it is all about combining strings together, but not by using the + operator. String Interpolation works only when we use double quotes (“”) for the string formation.
String Interpolation provides an easy way to process String literals. String Interpolation refers to substitution of defined variables or expressions in a given String with respected values. This is how, string Interpolation works, it executes whatever that is executable. Let’s see how to execute numbers and strings.
In Elixir, string interpolation calls the Kernel.to_string/1 macro, which evokes the String.Chars protocol. By default, it handles strings, atoms (including nil, true, false and module name aliases like String – which are all just atoms behind the scenes), integers, floats, and some lists.
Eventually, the objects (1, 2) created in this process gets terminated as their are of no use. In String Interpolation, it creates an only one object for (“hela “+string+” puri”) and embeds the existing string (weds)to it.
%{ ... }
is a string literal. It's similar to "..."
.
%{a string} == "a string"
# => true
#{expr}
inside those string literal is interpolation. An expression expr
inside the substituted with the value of it. For global variable you can omit {
and }
.
"#{1 + 2}"
# => "3"
%{#$"} == $".to_s
# => true
$"
is one of pre-defined variables: an array of loaded module names.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With