I have a string like this in Ruby
word=0 id=891efc9a-2210-4beb-a19a-5e86b2f8a49f
How can I get both the word and id values from that string?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here’s an approach that will work – split up the target string into tokens by whitespace and equals-sign characters (
tokens=str.split(/[= ]/)) and create a Hash out of them (Hash[*tokens]). The result is aHashwhose keys are the token before the equals sign and the values are the ones after it:Of course, it will break if your “keys” or “values” contain equal signs or spaces but it works for your example.