How to insert variable into formatted text in ruby [duplicate] - ruby

This question already has answers here:
Ruby single and double quotes
(6 answers)
Closed 4 years ago.
So I have a string formatted with triple quotation marks or %q{} like this and I want to insert a variable into the string; apparently using #{variable} wouldn't work.
variable = "some string"
puts %q{
My string looks like
this and i want to show
my string here #{variable}
}

Just use uppercase "Q"
variable = "some string"
%Q{ My math #{1+1} and string #{variable}}

Related

Are there any differences between "string" and 'string' in ruby? [duplicate]

This question already has answers here:
Double vs single quotes
(7 answers)
Closed 2 years ago.
I am newbie in Ruby.
Are there some differences between "string" and 'string' in ruby?
such as
txt_data.gsub("ABC",'')
and
txt_data.gsub('ABC','')
It seems they are always the same.
Are there some cases where we need to use "" and '' selectively?
The difference is, you could interpolate the value when you use "" but you can't when you use ''
See the below example
value=23
a="The value is #{value}"
p a
a='The value is #{value}'
p a
Output
"The value is 23"
"The value is \#{value}"

Why does single quote not work with string interpolation in ruby [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I tried string interpolation with single quotes using #{} but it does not work. When I tried it with double quotes it works. Please explain to me why this is so, if it's possible to do string interpolation with single quotes and how to do so in ruby if it is possible.
Ruby doesn't interpret single-quoted strings.
This might seem like a limitation at first, but it's actually a nice feature. It allows you to enter many characters without having to escape them, which results in more legible code:
file = 'C:\foo\bar\baz.txt'
# as opposed to:
file = "C:\\foo\\bar\\baz.txt"
Or when having a string about string interpolation itself: (note that Stack Overflow's syntax highlighting is misleading – there's no interpolation)
string = 'In Ruby, you can write "1 + 2 = #{ 1 + 2 }" to get "1 + 2 = 3".'
# instead of:
string = "In Ruby, you can write \"1 + 2 = \#{ 1 + 2 }\" to get \"1 + 2 = 3\"."
Apart from '...' and "...", Ruby also has %q(...) and %Q(...) style string literals (the former without, the latter with interpolation). This is especially useful if your string contains both, single and double quotes:
string = %q(A string containing '...' and "...")
You can even pick your own delimiter: (again, the syntax highlighter can't keep up)
string = %q#A string containing '...', "..." and (...)"#
And finally, you can mix and match different string literal styles:
string = %q(foo) 'bar' "baz"
#=> "foobarbaz"
This is how ruby language is designed.
From language docs: Literals
Double-quote strings allow escaped characters such as \n for newline,
\t for tab, etc.
Double-quote strings allow interpolation of other
values using #{...}:
value = 10
puts "Test value is #{value}"
# => Test value is 10
Interpolation may be disabled by escaping the “#” character or using single-quote strings
puts 'Test value is #{value}'
# => Test value is #{value}
Single-Quoted Strings and Escapes/Expressions
That's just how the language is defined: you can't interpolate using the embedded expression operator (#{}) within a single-quoted string. Unlike double-quoted strings, single-quoted strings aren't scanned for embedded expressions or most escapes. This is important when you need to do things like printing escape characters or unevaluated expressions such as:
puts 'This is a newline character: \n'
puts 'This is how you embed an expression: #{foo}'
That doesn't mean you can't approximate interpolation with single-quoted strings in other ways. For example:
'foo: %s' % 'bar'
#=> "foo: bar"
sprintf '%d, %d, %d', 1, 2, 3
#=> "1, 2, 3"

Ruby unescape hex code in string [duplicate]

This question already has answers here:
How do I encode/decode HTML entities in Ruby?
(8 answers)
Closed 5 years ago.
Does Ruby have a method to unescape hex codes within a string?
For example:
string = "Plus minus symbol: ±"
How can I print that string with the hex code replaced with the actual character: "±"? I'm looking for a generic solution that works for any hex code.
You could use Nokogiri to parse the symbol and then add it to your string:
require 'nokogiri'
symbol = Nokogiri::HTML.parse("±").text
#=> "±"
string = "Plus minus symbol: #{symbol}"
#=> "Plus minus symbol: ±"

Why does ruby automatically combine Strings? [duplicate]

This question already has answers here:
Where is Ruby's string literal juxtaposition feature officially documented?
(4 answers)
Ruby backslash to continue string on a new line?
(3 answers)
Closed 8 years ago.
If I have this code:
a = "hi" "pie"
puts a
It will print out hipie. Does Ruby automatically combine these?
Yes. From Literals: String
Adjacent string literals are automatically concatenated by the interpreter:
"con" "cat" "en" "at" "ion"
#=> "concatenation"
"This string contains " "no newlines."
#=> "This string contains no newlines."

How can I test whether a string only contains characters in a given set? [duplicate]

This question already has answers here:
Validate that string contains only allowed characters in Ruby
(4 answers)
Closed 2 years ago.
Given a string of a mobile phone number, I need to make sure that the given string only contains digits 0-9, (,),+,-,x, and space. How can I do it in Ruby?
Use:
/^[-0-9()+x ]+$/
E.g.:
re = /^[-0-9()+x ]+$/
match = re.match("555-555-5555")
if (/^[-\d()\+x ]+$/.match(variable))
puts "MATCH"
else
puts "Does not MATCH"
end
Use String#count:
"+1 (800) 123-4567".count("^0-9+x()\\- ").zero? # => true
"x invalid string x".count("^0-9+x()\\- ").zero? # => false

Resources