This question already has answers here:
How do I convert this Ruby String into an Array?
(6 answers)
Closed 6 years ago.
My string is
"[1,2,3]"
I want to get output in the below format
[1,2,3]
I want to extract array present in my string.
Just parse it with JSON.parse method like this ;
JSON.parse("[1,2,3]")
You can use eval:
try:
eval("[1,2,3]")
=> [1, 2, 3]
Ok, try it:
"[1,2,3]".scan(/\w/).map{|x| x.to_i}
Related
This question already has answers here:
How do I encode/decode HTML entities in Ruby?
(8 answers)
Closed 6 years ago.
How can I extract the ascii code from a string and then convert it to char?
string = "a – b"
to
a – b
Use CGI library from ruby
CGI.unescapeHTML("a – b")
#=> "a – b"
This question already has answers here:
How do I convert a String object into a Hash object?
(16 answers)
Closed 8 years ago.
I have a string. {"response_code"=>"SUCCESS", "authorisation_result"=>"Approved", "transaction_number"=>"1234567", "receipt_number"=>"999999", :cents=>100} that is stored as a value in hstore in postgresql.
So it's a raw string not a YAML or a JSON.
I was wonering how to get the value back to the hash that was used to insert the record.
The idea of using EVAL scares the hell out of me, because there is potential for use input here.
This question might seem to be a replica of How to convert a string to a hash in ruby/rails without using eval?, but the answer there doesn't mesh with the question. It only offers eval as the solution. (I don't understand how the answer was marked as the answer)
Ruby hashes look pretty similar to JSON, so this would work for your example:
require 'json'
str = "{\"response_code\"=>\"SUCCESS\", \"authorisation_result\"=>\"Approved\", \"transaction_number\"=>\"1234567\", \"receipt_number\"=>\"999999\", :cents=>100}"
JSON.parse str.gsub(/:(\w+)/){"\"#{$1}\""}.gsub('=>', ':')
# => {"response_code"=>"SUCCESS", "authorisation_result"=>"Approved", "transaction_number"=>"1234567", "receipt_number"=>"999999", "cents"=>100}
This question already has answers here:
Convert erlang terms to string, or decode erlang binary
(2 answers)
Closed 9 years ago.
Is there a way how to convert a tuple to a string ?
Consider I have the following list :
[{atom,5,program},{atom,5,receiving},{nil,5}]
I wish to convert this into the following string:
"{atom,5,program},{atom,5,receiving},{nil,5}"
I have tried using erlang:tuple_to_list on each element in the list, which returns
A = [atom,5,program]
Eventually, I can't concatenate that with "{" ++ A ++ "}"
Any ideas how I can turn that to a string ?
Term = [{atom,5,program},{atom,5,receiving},{nil,5}].
lists:flatten(io_lib:format("~p", [Term])).
This question already has answers here:
Ruby: Split string at character, counting from the right side
(6 answers)
Closed 9 years ago.
I want to be able to split strings into 2 elements, because each string would contain at least one delimiter.
Example: "hello_world". If I apply .split("_") then I receive: ["hello", "world"].
The problem arises when I have a string with two or more delimiters. Example "hello_to_you".
I want to receive: ["hello_to", "you"].
I know about the limit option for the split function: .split("_", 2), but it produces: ["hello", "to_you"].
So, basically, I would need to split the entire string ONLY with the last delimiter ("_").
This is exactly what String#rpartition does:
first_part, _, last_part = 'hello_to_you'.rpartition('_')
first_part # => 'hello_to'
last_part # => 'you'
try
'hello_to_you'.split /\_(?=[^_]*$)/
class String
def split_by_last_occurrance(char=" ")
loc = self.rindex(char)
loc != nil ? [self[0...loc], self[loc+1..-1]] : [self]
end
end
"test by last_occurrance".split_by_last #=> ["test by", "last"]
"test".split_by_last_occurrance #=> ["test"]
This question already has answers here:
Array.join("\n") not the way to join with a newline?
(7 answers)
Closed 5 years ago.
I'm having an issue which I can't seem to solve. I have an array which I need to convert to an single string. The elements need to be put underneath each other.
sample_array = ['a','b','c','d','e']
desired output:
sample_array = "a
b
c
d
e"
I thought I could do this with a 'heredoc', but I can only get the elements behind each other inline. This is unfortunately not what I need. Anyone who can help me?
edit for edit question
In a single line, you can use inject:
sample_array = ['a','b','c','d','e']
puts sample_array.inject(""){|conc,x| conc + "\n" + x }
=> "a b c d e"
that will fold the array recursively and adding a line between chars