I posted this at RMRK a couple of days ago. This is just something awesome about Ruby I noticed, I can’t really think of any practical uses for this. It’s basically just harnessing the String.to_i(base) function that Ruby comes built-in with. I created a method that makes it simpler, and fixes some bugs with converting to base 1. The way it works is:

String.convert_base(from, to) – where String is an instance of the String class (anything in quotation marks), even letters, where from is the base you are converting from, and to is the base you’d like it to convert to. Of course, this only works up to base 36 (ten digits + 26 letters), and down to base 1, because other bases are kinda impossible without hard-coding them in. And they wouldn’t be practical at all anyway.

Here are some examples: “38”.convert_base(10, 2) -> 100110. This has converted 38 from decimal to binary. “”.convert_base(2, 10) -> 284. This has converted  from binary to decimal. “Pacman”.convert_base(36, 10) -> 1529039327. This has (awesomely) converted “Pacman” from base 36 (where all letters of the alphabet have a numerical value) to decimal. “2073739462”.convert_base(10, 36) -> “yanfly”. This has converted those numbers into letters.

Anyway, you can get the snippet here. Play around with it, whatever. Note that it’s incredibly cool, because if you’re a nerd like me you’ll have fun converting numbers to letters and vice-avers with from = 36 and to = 10, or the other way around. That is all. I’m a bit behind on my scripting, sorry about that. Turns out school’s a bitch. Yanfly’s taking a well-deserved scripting break, so I might do some catching up while he does that. I’ve got holidays coming up soon, so I anticipate some scripting then.

Enjoy, I’ll probably be back with a script next time.

Advertisements