私は文字列を持っています:
parsed[tag] => "first_name: 'Richard'"
次のようなコードを実行する必要があります。
client.put_if_absent("profiles", bot_client_name, {first_name: 'Richard'})
その最初の文字列を使用して、同等のテキストのコードとして機能することは可能ですか?使っeval(parsed[tag])
てみましたが失敗でした。
私は次のように変更しました:
response = "client.put_if_absent('profiles', bot_client_name, {#{parsed[tag]}})"
eval(response)
これは実際に機能します---潜在的なリスクを減らすために何ができますか?
ユーザー入力の解析にevalを使用しないでください。それは非常に危険であり、安全になることは決してありません。ここでのあなたの本当の目標は、この文字列"first_name: 'Richard'"
を取得し、それからこのオブジェクトを作成することであるように見えます{first_name: 'Richard'}
。そのタスクを実行するために評価は必要ありません。
If you're trying to take user input, and turn it into an object hash, checkout this question. How do I convert a String object into a Hash object? I recommend looking at jackquack's answer in that post to give you an idea about how you can use the methods in Ruby's powerful String library (http://www.ruby-doc.org/core-2.1.4/String.html) to parse your input.
If your user input is in a standard format like JSON, YAML, XML, etc. then you can use a library that will do all this hard work for you. If your user input is in a custom format, such as seems to be your case, then you need to roll that parsing on your own.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加