Get emoji flag from country code in Ruby - ruby

I want to convert a country code like "US" to an Emoji flag, ie transform "US" string to the appropriate Unicode in Ruby.
Here's an equivalent example for Java

Use tr to translate alphabetic characters to their regional indicator symbols:
'US'.tr('A-Z', "\u{1F1E6}-\u{1F1FF}")
#=> "🇺🇸"
Of course, you can also use the Unicode characters directly:
'US'.tr('A-Z', '🇦-🇿')
#=> "🇺🇸"

Here is a port of that to Ruby:
country = 'US'
flagOffset = 0x1F1E6
asciiOffset = 0x41
firstChar = country[0].ord - asciiOffset + flagOffset
secondChar = country[1].ord - asciiOffset + flagOffset
flag = [firstChar, secondChar].pack("U*")

Related

How to print/scan Underscores in Numeric Literals

Team,
I am not able to use the Java 7 Underscores in Numeric Literals feature for getting the input from user and printing out in same format as declared. Please help in doing that? OR Is this feature is incomplete?
Scanner input = new Scanner( System.in );
int x = 1_00_000;
System.out.print( "Enter numeric literals with underscores: " ); //2_00_000
x = input.nextInt(); //java.util.InputMismatchException
System.out.println(x); // Prints in normal format, but want to be in 2_00_000.
NOTE: In Eclipse; I am able to change the value of numeric literal with Underscored numeric literal in runtime. This may be hack, but this is needed feature to input Underscored numeric literal in runtime rit?.
http://www.eclipse.org/jdt/ui/r3_8/Java7news/whats-new-java-7.html#miscellaneous
if you want maintain the underscores you can use String:
Scanner input = new Scanner( System.in );
System.out.print( "Enter numeric literals with underscores: " ); //2_00_000
String stringLiterals = input.nextLine();
System.out.println(stringLiterals); // Prints 2_00_000.

How do I use Base64 and exclude special characters +/= from string?

I am currently using Ruby's 'base64' but the strings that are created have special characters like /+= .
How do I remove these and still make sure that my decode works in the future?
Essentially I want alphanumeric to be used.
Rather than invent something new, I'd use Base64.urlsafe_encode64 (and its counterpart Base64.urlsafe_decode64) which is basically base64 with + and / replaced with - and _. This conforms to rfc 4648 so should be widely understandable
If you want alphanumeric, I think it is better and is practical to use base 36. Ruby has built-in encoding/decoding up to base 36 (26 letters and 10 numbers).
123456.to_s(36)
# => "qglj"
"qglj".to_i(36)
# => 123456
class Integer
Base62_digits = [*("0".."9"), *("a".."z"), *("A".."Z")]
def base_62
return "0" if zero?
sign = self < 0 ? "-" : ""
n, res = self.abs, ""
while n > 0
n, units = n.divmod(62)
res = Base62_digits[units] + res
end
sign + res
end
end
p 124.base_62 # => "20"
This could be adapted to handle lower bases, but it may be sufficient as is.

how to remove whitespace but not utf-8 character in ruby

I want to prevent users to write an empty comment (whitespaces, , etc.). so I apply the following:
var.gsub(/^\s+|\s+\z|\s* \s*/.'')
However, then a smart user find a hole by using \302 or \240 unicode characters so I filtered out these characters too.
Then I ran into problem as I introduced several languages support, then a word like Déjà vu becomes an error. because part of the à character contains \240. is there any way to remove the whitespaces but leave the latin characters untouched?
A way around this is to use iconv to discard the invalid unicode characters (such as \230 on its own) before using your regexp to remove the whitespaces:
require 'iconv'
var1 = "Déjà vu"
var2 = "\240"
ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
valid1 = ic.iconv(var1) # => "D\303\251j\303\240 vu"
valid2 = ic.iconv(var2) # => ""

What is the Ruby equivalent of preg_quote()?

In PHP you need to use preg_quote() to escape all the characters in a string that have a particular meaning in a regular expression, to allow (for example) preg_match() to search for those special characters.
What is the equivalent in Ruby of the following code?
// The content of this variable is obtained from user input, in example.
$search = "$var = 100";
if (preg_match('/' . preg_quote($search, '/') . ";/i")) {
// …
}
You want Regexp.escape.
str = "[...]"
re = /#{Regexp.escape(str)}/
"la[...]la[...]la".gsub(re,"") #=> "lalala"

How to get GL, G0 from "GL=>G0" using Ruby Regular Expression

I have following string:
"xxxxx GL=>G0 yyyyyy "
I want to extract GL and G0 using ruby regular expression.
Thanks.
Well, this is rather vague. Do you want to pull out key/value pairs when separated by => ?
The following regexp may suit your needs:
matches = /.*(\w{2})=>(\w{2}).*/.match("xxxxxx GL=>G0 yyyyy ")
puts matches[1] // GL
puts matches[2] // G0
This assumes that your key/values are 2 characters long separated by a => sign. It does not permit spaces between the characters and the => sign. Let me know if this is what you need. Otherwise, provide a more detailed description of what strings you may need to parse.

Resources