how to access hash inside a string [closed] - ruby

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm reading a CSV file. It contains a column as
{"doctype"=>"birthrecord", "records"=>[{"pagenum"=>"5", "recordId"=>"7", "tagGroups"=>[{"data"=>{"first"=>"given_name", "given_name"=>"Severiano ", "surname"=>"Bustamante"}}]}]}
The header of this columns is Output. When I do row["Output"] its returning sting of hash as
"{"doctype"=>"birthrecord", "records"=>[{"pagenum"=>"5", "recordId"=>"7", "tagGroups"=>[{"data"=>{"first"=>"given_name", "given_name"=>"Severiano ", "surname"=>"Bustamante"}}]}]}"
How can I access the hash like normal hash?
Can anyone help me please.

The dataset looks almost like JSON except => is used in place of :, so replacing them you can now adequately parse it like a JSON object
JSON.parse('{"doctype"=>"birthrecord", "records"=>[{"pagenum"=>"5", "recordId"=>"7", "tagGroups"=>[{"data"=>{"first"=>"given_name", "given_name"=>"Severiano ", "surname"=>"Bustamante"}}]}]}'.gsub("=>", ":"))["records"][0]["tagGroups"][0]["data"]
#=> {"first"=>"given_name", "given_name"=>"Severiano ", "surname"=>"Bustamante"}

Related

Replacing memory references/addresses from a string containing <ClassName:MEM_REF> type parts [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have strings like:
NoMethodError: undefined method 'sort_by!' for #<Hash:0x00007f98f03c84e0>
These strings can contain n number of such parts: <Hash:0x00007f98f03c84e0>.
Here, 0x00007f98f03c84e0 is just a placeholder of memory reference. And also Hash is type of object of which this memory reference is. There is no need to discuss how these strings got formed but in the end i have strings which can have anything like <ClassName:MEM_REF> and i have to replace MEM_REF part.
Going back to my original example, I want to remove this memory ref part 0x00007f98f03c84e0 with any string of my liking. Again, 0x00007f98f03c84e0 is an example, it will be any arbitrary memory address.
Looking for an elegant way of doing this in ruby.
Try following regex in ruby console, should work: /:[0-9]x[0-9A-Za-z]*(?=>)/.
And to mask these refs with anything else, try input_string.gsub!(/:[0-9]x[0-9A-Za-z]*(?=>)/, "REPLACE_TEXT")

Parse form using Nokogiri and pass it to URI.encode_www_form? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have simple HTML form, which I got from a web page:
<form id="my">
inputs....
</form>
I need to get this form via it's ID, which I know how to do:
#get_doc = Nokogiri::HTML(page)
nb = #get_doc.at_css('#my')
maybe could i iterate via object ?
I need to get all the input values and input names into some variable, and then pass it to URI.encode_www_form.
How can I do this? How could I get all the inputs inside the form with names and values, and pass them to encode_www_form?
arr = []
# form = doc.at_css '#form'
form.css('input').each do |i|
arr << [i['name'], i['value']]
end
URI.encode_www_form arr

Ruby Strings, Loops, and Arrays [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
If given an array l = Array.new with l being filled with text of an essay.
What would be the easiest method to create a new string, loop through each line of the array and then add each line of the array to the newly created string?
my_string = l.join '' should do the job fine.
A more imperative solution is the following:
my_string = ''
l.each do |line|
my_string += line
end

Prawn: how to remove all borders from a table? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm working with Prawn gem and creating a table. I want to remove all its borders. I can't find this facility in the documentation.
How do I do that?
Here is the documentation for the prawn gem tables.
According to the "Constructor Details" section, you can pass a variety of :border_<x> options to the constructor... I'd suggest trying to pass a value of :border_width => 0 to your table constructor. (note: not tested - try it yourself and see what happens)

How do I write Ruby in terminal to extract twitter handles from a paragraph of text? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
If I have a paragraph of #text and I want to extract the #twitter handles from it and make them into a list, how would I do this?
Use String#scan
Use the String#scan method to extract your expression into an array. For example:
str = <<'EOF'
If I have a paragraph of #text and I want to extract the
#twitter handles from it and make them into a list, how
would I do this?
EOF
str.scan /#\S+/
#=> ["#text", "#twitter"]
Assuming your #text is a string, use regex to extract #twitter handles and add them into an array.

Resources