Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Dumb question

As always... xD

I hate to admit, but i need some help...

I´m making a file loader, some kind of configuration file loader. The purpose is to make it the easier possible, and permit some customizations that i have in mind. I´ll be using it for a script i´m making, and soon i´ll release it in .org (and then the need for it to be as easy as possible).

In it i have blocks and properties, and those properties can have one value that can be either a Numeric or String. I want the syntax used to be exact like in real Ruby coding. When opening the txt file with File.open("name.txt", "rb"), it opens everything as a string.

Hell, until that point no problem. I can identify integers, floats and strings very well, and transform/convert them properly (like taking the quotes for a string and transforming "1.0" into 1.0 for example. But characters like \n, \t, \c, \blabla[text] appears as \\n \\t, \\c, \\blabla[text], and in fact i want them to appear as they´re in the txt file. That was expected, and i had a solution for that but... Haven´t worked lol -_-

The question is... for example, to transform a "\\n" into "\n", what do i have to do? I tried:
Code:
"\\n".sub!(/\\\\/, '\\')
"\\n".sub!(/\\\\/, "\\")
But with no result after all.

P.S.: I´m doing this for a database/configuration-like system... My system may have lots of flaws and all, and it´s done to be used by other scripters. May i use XML? But for that i need that everyone that uses it has Ruby installed, so i´m clueless on what´s better in terms of reliability AND easiness ^^ And surely, with XML it will become more difficult to make those config files, but they will look more professional, indeed.

Some help plz ^^
 
Well i tried it with my Ruby installation and it didn´t worked. Maybe that interpreter is a bit different. I have Ruby 1.8.5 here, and even in RMXP (1.8.2) it didn´t worked.

I´m starting to think that it´s not possible...
 
In truth i can quite understand why it doesn´t work. '\' is a symbol used to start metacharacters. "\\" is the metacharacter used for the common backslask ('\'), so when we try to substitute "\\\\" (2 backslaskes") by "\\" (one backslash), it does (even if it´s not what we want), "\n" is another metacharacter, a complete character like "a" or "1". That '\' in it can´t be splitted because for Ruby it represents a line return (a byte or two, to be exact), and so it´s ONE character. If we have to substitute/take it off with Regexp, we have to mess up with it all. The same happens for "\\\\". In the string "\\n" it has two characters: a backslash and a line return. When we try to search for a double backslash it doesn´t find and return nil, because it doesn´t quite exist in the string.

To achieve that, we should mess up with strings' bytes...
Code:
string = "Hey!\\nI'm here!"
string[4, 2] = "\n"  # With double quotes
p string    # => "Hey!\nI'm here!"
I tested it and worked. The disadvantage is that you can´t add conditional characters. Like...
Code:
string = "Hey!\\nI'm here!"
letter = "n"
string[4, 2] = "\#{letter}"
p string    # => "Hey!\#{letter}I'm here!"
Or...
Code:
string = "Hey!\\nI'm here!"
string[4, 2] = "\\#{letter}"
p string    # => "Hey!\\nI'm here!"
This means you have to know exactly what and why you´re substituting the two characters.

It´s a bit complicated, i admit... But i think i´ve understood why it didn´t worked and found a solution to my problem. By other means it´s resolved BUT I REALLY want to listen to other points of views (so please wait to close it ^^). You can argue on my explanation too, i just want to discuss about it and find a better solution, because it´s not so reliable to mess up with string bytes when you want just to store text.

So... Comments? Suggestions?
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top