Ruby unescape hex code in string [duplicate] - ruby

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: ±"

Related

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

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

Capitalize, swapcase and reverse in Ruby [duplicate]

This question already has answers here:
Ruby modify a piece of a string
(5 answers)
Closed 9 years ago.
I have a challenging Ruby problem:
I want to convert "howdy" to "YDWOh" and "how are you" to "uoy era WOh".
which is essentially capitalize, swapcase and reverse functions. Capitalize and swapcase is only for the first word of the sentence. How do I do this when I have more than one word in the sentence?
Try the following steps:
capitalize your string
split(' ') your string into an array with words
Use each_with_index.map to go over each word in the array
Use swapcase on only the first word of the array
join(' ') the array back into a string
reverse the string
You can try the below:
a = "how are you"
p a.gsub(a.split[0...1].join(' '),a.split[0...1].join(' ').capitalize.swapcase).reverse
Output:
"uoy era WOh"

How to convert a unicode string to its symbol characters in Ruby?

I have the following string:
Ruby :: \u041D\u043E\u0432\u0438\u043D\u0438
My question is how to convert it to utf8 characters (in my case cyrilic letters)?
In Ruby 1.9:
"\u041D\u043E\u0432\u0438\u043D\u0438".encode("UTF-8")
=> "Новини"

Resources