2

How i can convert string into hash?

now i use:

eval "{'1627207:28320'=>'text'}"
=> {'1627207:28320'=>'text'}

but "eval" is not good for my case - string passed from params, and such case it is not secure

Edited:

passed string can also be:

"{'1627207'=>'text', '11:167:28320'=>'text 1 / text 2 / unicode=>привет!'}"

Then need result hash:

{'1627207:28320'=>'text',
'11:167:28320'=>'text 1 / text 2 / unicode=>привет!'}
2
  • 1
    stackoverflow.com/a/9910355/1275603 maybe something like that Commented May 2, 2012 at 8:55
  • I guess the real question is, why are you letting your users pass Ruby code to your application? Why not a format that you can parse safely, like JSON? Commented May 22, 2014 at 17:00

4 Answers 4

4
str = "{'1627207:28320'=>'text'}"
p Hash[*str.delete("{}'").split('=>')] #{"1627207:28320"=>"text"}

edit for different input:

str = "{'1627207:28320'=>'text', 'key2'=>'text2'}" 
p Hash[*str.delete("{}'").split(/=>|, /)] #{"1627207:28320"=>"text", "key2"=>"text2"}
Sign up to request clarification or add additional context in comments.

1 Comment

it is fine, but user can pass "{'1627207:28320'=>'text', 'key2'=>'text2'}" string. Then error: ArgumentError: odd number of arguments for Hash from (irb):in []'
2
class String
  def to_h
    h={}
    self.scan(/'(\w+.\w+)'=>'(\w+)'/).each { |k,v| h[k]=v }
    h
  end
end

p "{'1627207:28320'=>'text','test'=>'text2'}".to_h
=>{"1627207:28320"=>"text", "test"=>"text2"}

EDIT: shorter version

class String
  def to_h
    Hash[self.scan(/'([^']+)'=>'([^']+)'/)]
  end
end

2 Comments

worked for: "{'key'=>'texttext' }" but not worked for "{'key'=>'text text' }"
well, the regex needs to be oriented towards the kind of inpout that needs to be processed, adapted the shorter version to look for the char ' as delimiter
0

Quite straight forward:

$ irb
irb(main):001:0> k='1627207:28320'
=> "1627207:28320"
irb(main):002:0> v='text'
=> "text"
irb(main):003:0> h={k => v}
=> {"1627207:28320"=>"text"}
irb(main):004:0> h
=> {"1627207:28320"=>"text"}
irb(main):005:0>

5 Comments

But you don't parse string, just creating a hash from variables
What kind of parsing he does? In his example he gets from text to hash. If he gets an string looking like a hash then parsing is necessary as Grzegorz pointed out
He gets "{'some_key' => 'value'}" string, but also could get "{'first' => 1, 'second' => 2}", becouse both strings contains hash. So he need a code that could convert all string to hash
In that case parsing is needed, maybe he could use JSON.decode()
Parsing is necessary to interpolate the string content. Even you use eval, JSON or other libraries, they do the same thing.
0

You could simply try this:

text_hash['1627207:28320'] = 'text'
text_hash

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.