Get unicode by hex string in Ruby [duplicate] - ruby

This question already has an answer here:
Print Unicode escape codes from variable
(1 answer)
Closed 4 years ago.
Given a hex code, for example: 1f60d, how do I find the corresponding unicode (code point) 😍?

Convert the string to an integer (e.g. via hex) and the integer to a character via chr:
'1f60d'.hex.chr('UTF-8')
#=> "😍"

You could do that by using Array#pack:
["1F60d".to_i(16)].pack("U*")

You can use the \u{HEX} to accomplish the same.
puts "\u{1F60d}"

Related

How to split the string by certain amount of characters in Ruby [duplicate]

This question already has answers here:
Chop a string in Ruby into fixed length string ignoring (not considering/regardless) new line or space characters
(5 answers)
Closed 8 years ago.
How to split the string by certain amount of characters in Ruby?
For example, suppose that I have the following string:
some_string
and I want to split it by every 4th character, so the resulting strings will look like this:
som
e_s
tri
ng
How can I do it?
Thanks in advance.
Using Enumerable#each_slice
'some_string'.chars.each_slice(3).map(&:join)
# => ["som", "e_s", "tri", "ng"]
Using regular expression:
'some_string'.scan(/.{1,3}/)
# => ["som", "e_s", "tri", "ng"]

Strange string behavior in Ruby [duplicate]

This question already has answers here:
Where is Ruby's string literal juxtaposition feature officially documented?
(4 answers)
Closed 7 years ago.
So I was playing around in Ruby, and noticed that "a""b" returns "ab". If fond this very strange and useless, so I was wondering what this is called and if it has a purpose. I would appreciate any answers. Thanks!
This is called "string literal concatenation" and it is common in many languages. More specifically, adjacent string literals that are not separated by any other operators are automatically concatenated together. They may be considered to be just one string literal.
This exists in C, C++, Python, and Ruby to name a few.
MSDN: String Literal Concatenation (C)
Lexical Analysis: String literal concatenation (Python)
Where is Ruby's string literal juxtaposition feature officially documented? (Ruby)
Literals - Strings (Ruby)
An example of where this might be used is to break up a long string onto multiple lies, also adding the ability to comment each piece. Something I wrote in Python the other day:
hdr = struct.Struct('<'
'8s' # 0x00 Magic value
'I' # 0x08 Offset
'I' # 0x0C Length
'H' # 0x10 Type
'H' # 0x12 Flags
) # 0x14 (Total)
Note that this method takes just one parameter, a string, and I didn't manually concatenate the pieces.
I've never noticed this before, and this looks to be another form of concatenation like << and +.

Ruby << for String [duplicate]

This question already has answers here:
What are <-- Ruby Strings called? And how do I insert variables in them?
(3 answers)
Closed 8 years ago.
Can someone tell me the name of
<<-MAP
STRING HERE
MAP
operator (<<-) in ruby? I tried search for 'double less than' but it didn't turn up anything. I want to learn more about it but don't even know what it's called!
Thanks
Thats called the here doc syntax .Generally used to enter multiline strings. You can read about it here http://blog.jayfields.com/2006/12/ruby-multiline-strings-here-doc-or.html
and also here The <<- operator on Ruby, where is it documented?
It's not an operator, it's a here document (aka heredoc) String literal. It works more or less like heredocs in other languages.
It is specified in section 8.7.6.3.6 of the ISO Ruby Language Specification.

How to generate a UTF-8 character with Ruby [duplicate]

This question already has answers here:
Convert unicode codepoint to string character in Ruby
(2 answers)
Closed 8 years ago.
I am using Ruby, and I need to generate one UTF-8 character by an integer.
eg: if id = 65(10), I need to get the UTF-8 char which is encoded 00a1.
Is this what you're looking for?
65.pack('U')

How to remove unicode in Objective-C [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Replace letters in a string
I have some texts "anh yêu em"
I want to convert to this text "anh yeu em".
Does anyone know to convert this text "anh yêu em" to "anh yeu em" in the Objective-C?
Many thanks,
You can use NSString dataUsingEncoding to convert vietnamese unicode character to equivalent ascii character.
For details, pls follow this url nsstring-unicode-to-ascii-equivalent

Resources