Why does ruby automatically combine Strings? [duplicate] - ruby

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."

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}"

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

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}}

Ruby - Extract data from string with regex [duplicate]

This question already has answers here:
How to extract URL parameters from a URL with Ruby or Rails?
(10 answers)
Closed 7 years ago.
In ruby I have string that looks like this:
"\/v1\/195900\/patients?DEPARTMENTID=162&GUARANTORCOUNTRYCODE3166=1&offset=20"
how can I extract offset value from this string with regular expressions?
It doesn't satisfy your requirement to use a regex, but here is a way:
uri = "\/v1\/195900\/patients?DEPARTMENTID=162&GUARANTORCOUNTRYCODE3166=1&offset=20"
require "uri"
URI.decode_www_form(URI(uri).query).assoc("offset").last
# => "20"
or
URI.decode_www_form(URI(uri).query).to_h["offset"]
# => "20"
Assuming offset will always be present as offset= and it will always be a numeric value
str = "\/v1\/195900\/patients?DEPARTMENTID=162&GUARANTORCOUNTRYCODE3166=1&offset=20"
str.scan(/offset=(\d+)/)
#=> [["20"]]

Ruby: What does the "!~" operator mean? [duplicate]

This question already has answers here:
What does the !~ method do with String in Ruby
(2 answers)
Closed 8 years ago.
When declaring syntax such as:
a !~ b
where a,b are variables, what does it mean?
It is negation of =~, a regex match.
"a" !~ /b/
# => true
It is useful when you want to check whether a string does not match a certain pattern. For example, if you want to check if string s includes only numbers, then you can do:
s !~ /\D/

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