How to convert specific format array into hash [closed] - ruby

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have an array:
ar = ["p=abhZRAh7Un", "a=2", "c=1", "l=3033102", "r=1", "rt=mr", "pid=136330865", "pdid=AiOIhH2vzMPqvhYkxXOxeA%3D%3D", "lks=54.0311", "fks=54.0311"]
and need to convert it into a hash with keys p, a, c, etc. and values – whatever is to the right of the equal sign. What is an elegant way to do that in ruby?

Hash[ar.map{|s| s.split("=")}]

require 'cgi'
ar = ["p=abhZRAh7Un", "a=2", "c=1", "l=3033102", "r=1", "rt=mr", "pid=136330865", "pdid=AiOIhH2vzMPqvhYkxXOxeA%3D%3D", "lks=54.0311", "fks=54.0311"]
CGI.parse(ar.join('&'))
outputs:
=> {"rt"=>["mr"], "fks"=>["54.0311"], "pid"=>["136330865"], "lks"=>["54.0311"], "pdid"=>["AiOIhH2vzMPqvhYkxXOxeA=="], "r"=>["1"], "l"=>["3033102"], "c"=>["1"], "a"=>["2"], "p"=>["abhZRAh7Un"]}

Related

How do I combine two attributes using uniq? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Currently I am doing the following and was wondering if there was a way of merging this?
merged = list_with_objects_that_have_url_and_name_attributes
merged = merged.uniq{|ex| ex.url }
merged = merged.uniq{|ex| ex.name }
I'd like something like:
merged.uniq{|ex| ex.name || ex.url}
Not quite what you asked for, but it's compact:
merged.uniq!(&:url).uniq!(&:name)

Decoding a string [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I found out that many files on my web server contain these inputs:
K3SQCI5YoWjgHwLacjuuWrKs7yE=
yEw5FOQQ1LFUa3aBbKQJLqXTVGo=
I need to figure out the encryption type or what these inputs are.
It looks like a base64 encoded string, not an encrypted string. See http://en.wikipedia.org/wiki/Base64

Regular expression to find unknown acronyms [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I need a regular expression that will pick out lines with ALL CAPS or unknown acronyms.
ie: something like
/[A-Z]{2,}/
combined with
/(?!USA|UK|TLA)/
You mean something like this?
\b(?!(?:USA|UK|TLA)\b)[A-Z]{2,}\b
See it here on Rubular
\b is a word boundary to ensure that it will find complete words.

Comparing multiple values in an if statement - Ruby [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Can you compare multiple values in a single if statement in Ruby? If so, how? I know in other languages, you would just use a double ampersand (&&), but I have never seen any example or documentation about it, even in the official ruby documentation! Thanks for all the help in advance.
Yes, you can use the && operator
if a == 1 && b == 2
# do things
end
It IS in the documentation.
http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html#UG

How to modify string using RegEx? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
How do I transform the string from 'updown' to 'Up/Down' using RegEx?
If you already know the input and output string then there's no use case for regular expressions. Regular expressions are for matching a grammar, which is a set of strings, rather than a single string.
Are you looking for something more general? Perhaps your question is about how to split up the words up and down, or how to capitalize the letters?
string = "updown"
string.gsub!("up", "Up!")
string.gsub!("down", "Down")
string = "Up!Down"

Resources