Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Interpolation

Tags:

string

ruby

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!

like image 414
Alberto_Saavedra Avatar asked Oct 06 '14 14:10

Alberto_Saavedra


People also ask

What is an interpolation in Ruby?

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.

What does #{} do in Ruby?

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.

What is concatenation Ruby?

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.

How do you add a variable to a string in Ruby?

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.

What is ruby string interpolation?

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.

What is string interpolation in Python?

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.

How does string interpolation work in Elixir?

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.

What happens to the objects created during string interpolation?

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.


1 Answers

%{ ... } 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.

like image 200
falsetru Avatar answered Nov 04 '22 03:11

falsetru