3

So, for a game I'm working on, let's say I put a bit of text in the comment box of a spell:

<secondary effects: [["Flame", 15, true], ["Poison", 0.08, true]]>

By using the slice feature, I can reduce the text down to:

[["Flame", 15, true], ["Poison", 0.08, true]]

and then store it in the data variable, so

data = "[["Flame", 15, true], ["Poison, 0.08, true]]"

Now, how could I convert this text into code, so I could make a condition during the spell's damage process such as:

if data[0][2] == true then *something* end

Obviously, I can't use eval on the text in order to make it into data.

3 Answers 3

5

Your example is a JSON conform string (after fixing the missing "). Therefore, I would just use a JSON.parse:

data = '[["Flame", 15, true], ["Poison", 0.08, true]]'
#=> "[[\"Flame\", 15, true], [\"Poison\", 0.08, true]]"

require 'json'
JSON.parse(data)
#=> [["Flame", 15, true], ["Poison", 0.08, true]]
Sign up to request clarification or add additional context in comments.

3 Comments

This is the answer. Use a data language for data.
When I test this out in tutorialspoint dot com's execution test, this process works, but with the program I'm using, RPGMakerVXAce, it says 'No such file found' for json. When I read up on it, JSON is a JavaScript language interpreter, but in later years has been used in multiple programming languages. Perhaps this program uses an earlier version of Ruby, or it simply isn't included?
What version of Ruby do you use? Call ruby -v on the command line.
1

Whole idea of putting data into comments in Ruby is a smell. But first, let me literally answer your question: Any string is a code line in Ruby. To see whether it's a valid line, you just eval it:

string = "[{foo: 1}, {bar: 2}]"
data = eval string
data[0][:foo] #=> 1

Instead of trying to stuff code into comments, in Ruby we usually try to do the opposite: Stuff comments into code. More precisely, to write self-explanatory code that can be understood without comments. We call it literate programming.

Comments

0

Below is a very simple and quick solution.

data = eval('[["Flame", 15, true], ["Poison", 0.08, true]]')

Please make sure Flame has two quotes surrounding it but Poison don't. If it's a typo then above will work as it is but if not the case then you will need to add appropriate quotes to the any given string.

If you do not want to use eval then you can do something like below.

'[["Flame", 15, true], ["Poison", 0.08, true]]'.gsub(/[\[\]]/,'').split(",").each_slice(3).to_a

2 Comments

Suggesting eval without a warning about its usage isn't a good idea. Image when someone enters rm -rf / into that box...
Suggesting only convert your string to not the whole code. But if that is the case you can use something like this '[["Flame", 15, true], ["Poison", 0.08, true]]'.gsub(/[[]]/,'').split(",").each_slice(3).to_a , you can make solution more dynamic as you like.

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.