Advertisement
AngryPacman

Simple Ruby

Aug 3rd, 2011
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.79 KB | None | 0 0
  1. [center][b][size=18pt]Simple Ruby[/size][/b][/center]
  2. [center][b][size=14pt]Learning the language[/size][/b][/center]
  3.  
  4. Okay, both RPG Maker VX and XP run some form of RGSS, which stands for Ruby Game Scripting System. Most people see this system as 'go on to the internet, find a cool script and put it in the editor'. And that's okay, it's pretty much why we have scripters.
  5. But some people want to make an effort to learn the programming. Truth be told; it's not that hard. I went from modding a simple script to writing my own systems within about a month, without taking any tutorials.
  6. One thing I wish I did do, though, was learn simple ruby, not RGSS. Now that I've done this, I can help people get that one step closer to whatever it is they want out of this.
  7.  
  8. [hr]
  9. [center][b][size=14pt]Chapter 1: Maths and String[/size][/b][/center]
  10. [center][b][size=12pt]Lesson 1: Mathematics[/size][/b][/center]
  11.  
  12. [spoiler=Lesson 1]Open up VX or XP, it doesn't matter. Make a new, empty project. Nothing. Get the script editor up, by either pressing F11 or placing your preferred hand on your mouse or touch-pad, altering the position of your hand, thereby moving the cursor on your computer's screen, in a fashion that would take it to the icon of the pencil writing on the paper, and striking the left mouse button with your index finger (or middle finger, if you are burdened with left-handedness or you just like it that way), thus opening the script editor. I prefer pressing F11.
  13. I want you to delete EVERYTHING in the script editor. Yes, everything. You don't need any of these scripts, they're all utterly useless as of this moment. Now, enter the editor proper.
  14. Ruby is a very language centered around simplicity and elegance, or so I believe. As such, you can try out some impressive mathematics skills in the editor. If you enter one line, something like this:[code]1 + 2[/code]and run the game, guess what happens? Did you guess nothing? You're right!
  15. To make it display your super-amazing equation, you must add something. A command, that comes with Ruby, every installation. It's one of the most useful commands in the entire language. It is the almighty "p" function. Alternatively, you could use "print". Here's an example.[code]p 1 + 2[/code]Did you see that? Awesome! It knows what maths is. Ruby can perform pretty much every mathematical procedure you can think of. For multiplication, it uses * (asterisk), for division, it uses / (slash), addition and subtraction should be pretty obvious (+ and -), for exponents it uses ** (TWO asterisks), and for brackets it uses... umm... brackets. Give maths a shot now. Once you're done with that, move on to lesson 2.[/spoiler]
  16.  
  17. [hr]
  18. [center][b][size=12pt]Lesson 2: String[/size][/b][/center]
  19.  
  20. [spoiler=Lesson 2]If you know nothing about programming whatsoever, you won't have the faintest clue what in the world this 'String' thing is all about. In programming, at least. String is, quite basically, any series of characters contained within quotation marks, so that the computer can process it. "This is string." is a perfect example of string. What purpose does it serve here though?
  21. Urr... well, give it a shot! Try using the 'p' or 'print' command with your name. Without using string. I dare you. This is what I put:[code]p Tim[/code]I definitely get a prompt showing up, but it's an error. Oh no. It says: "Script '' line 1: NameError occurred. uninitialized constant Tim" WHAT DOES THIS MEAN?!
  22. It means that your name doesn't exist, really. However, if we put it in string, we get something completely different.[code]p "Tim"[/code]The prompt is much nicer now: it says my name and nothing else, like I wanted it to.
  23. That's all good and fancy, but what can we do with it? It's useless so far!
  24. Lots of cool stuff and lots more boring stuff can be done with string. Ever wanted to know what your name backwards is, but never had the time to check? Now's your chance. Use the reverse method on your name string to check. I'll show you what I mean:[code]p "Tim".reverse[/code]Hang on, why's there a dot there?!
  25. Good question, me. The period not only acts as a decimal point in mathematics and a punctuation mark in language, it is an method operand. Whatever method is written after the dot is performed on what is before the dot. So, let's give it a shot. Does it give you a prompt saying something like this?[code]"miT"[/code]If so, good! If not, reread the instructions.
  26. Well, we reversed our names. Fantastic. How many letters are in our names? I wonder! There's a way to find out, the length method. It goes a little something like this:[code]p "Tim".length[/code]That will print the number of characters in the string, my name. It will come with a prompt that says "3".
  27. My names not long at all. Let's multiply it. To see what's happening, I'll use 2 prompts this time.[code]p "Tim" * 5
  28. p ("Tim" * 5).length[/code]WHAT?! Let's take a look at this. I printed my name 5 times in the same prompt, i.e. "TimTimTimTimTim", and then I printed the number of characters in the print, 15. All good?[/spoiler]
  29.  
  30. [hr]
  31. [center][b][size=12pt]Chapter 1 Summary[/size][/b][/center]
  32.  
  33. [spoiler=Summary]So far, we've taken a look at the prompt, which we'll be using for pretty much this entire tutorial. We've demonstrated and learned about numbers and string; Ruby's mathematics and text objects. And though you might not know it, we've taken a look at 2 types of methods: English-language methods (reverse, length, p and print) and symbolic methods (*, +, -, /, "").
  34. You should be pretty comfortable with string, numbers and some simple methods by now, so let's do something uncomfortable.[/spoiler]
  35.  
  36. [hr]
  37. [center][b][size=14pt]Chapter 2: Errors, Arrays and Variables[/size][/b][/center]
  38. [center][b][size=12pt]Lesson 3: Stop, you fool![/size][/b][/center]
  39.  
  40. [spoiler=Lesson 3]If we can reverse string, we can reverse anything, right? So, try reversing a number. Just a number. You can trust me.
  41. [code]p 32.reverse[/code]No, you imbecile! You can't just do anything to anything! Reverse is a method that works for string, but not everything. The yummy error message you got was telling you that you can't just plonk down a number and reverse it. But you can't be bothered putting it into quotation marks, so here's a lazier way.[code]p 40.to_s.reverse[/code]._.
  42. The to_s method converts anything that isn't a string into string. Which means we can then reverse it. With me?
  43. The reason all this is happening is because while you can use methods on any object in Ruby, some methods only work on certain types of things. This is why we convert types using the built-in 'to' methods. To put it as simply as possible:
  44. to_s converts things into string.
  45. to_i converts things into integers (numbers).
  46. to_a converts thing into arrays.
  47. Slow down, man. What are arrays?[/spoiler]
  48.  
  49. [hr]
  50. [center][b][size=12pt]Lesson 4: Arrays[/size][/b][/center]
  51.  
  52. [spoiler=Lesson 4]Arrays are lists that are contained in square brackets []. Type in a command to print an empty pair of brackets. I used[code]p [][/code]because it's the most obvious one.
  53. CONGRATULATIONS! You've successfully created an empty list of nothing. Let's try putting something in there. Arrays store things in order. That's important; they won't change unless something changes them. That's right, they follow Newton's first law of motion. Spit out three random numbers of different values and put them in an array as such, in no specific order.[code][15, 93, 76][/code]Note that entries in an array are separated with a comma.
  54. Let's do something with this list. I want to know what the largest number in this array is, for some nondescript programming person-type reason. There's a built-in method that can be performed on arrays for that.[code]p [15, 93, 76].max[/code]That code will print out the largest number, in this case 93.
  55. That's getting rather annoying to retype, so let's do something to make it easier. At the top of the editor, type a line that says something like[code]list = [15, 93, 76][/code]Hang on, what did we just do?[/spoiler]
  56.  
  57. [hr]
  58. [center][b][size=12pt]Lesson 5: Variables[/size][/b][/center]
  59.  
  60. [spoiler=Lesson 5]We created a variable, that's what. This variable stores the array so we can play with it without typing everything every time. Hooray! To put this variable to the test, put a line [i]after you define the variable[/i] that says p list or whatever your variable is called. Success, the prompt came up with "[15, 93, 76]"! Fantastic.
  61. Now that we know that works, let's do something to appease my OCD. Let's sort the list. Use the sort! command to do this, i.e. [code]list.sort![/code]You don't even have to print it, though you can if you want to see its effect.
  62. Now, print the sorted list. For me, it came up with "[15, 76, 93]". It sorted the numbers numerically in ascending order.
  63. We had a list, we changed the list; the list has been changed. We changed a variable! Forever! Did you notice that the sort! method has a happy exclamation mark at the end, as if it's yelling at the interpreter to sort the list? Most Ruby methods have that exclamation mark if they change a variable for good. It doesn't change the method at all, it's just a recognizable naming protocol for the commands.[/spoiler]
  64.  
  65. [hr]
  66. [center][b][size=12pt]Chapter 2 Summary[/size][/b][/center]
  67.  
  68. [spoiler=Summary]We saw an error. Wasn't it scary? We looked at why it happened, and how to avoid it. We looked at lists; arrays. We can do simple methods to arrays (.max, .sort!) and can store them in a variable. We now know how variables work, how to change them and use them for convenience. Lastly, we understand why there is an exclamation point at the end of some methods.[/spoiler]
  69.  
  70. [center][b][size=12pt]Chapter 3: Advanced String[/size][/b][/center]
  71. [center][b][size=12pt]Lesson 6: Beautiful Poetry[/size][/b][/center]
  72.  
  73. [hr]
  74. [spoiler=Lesson 6]I'm such an amazing poet, I'm not even going to give you my poetry. You can have this stock boring poem.
  75. [code]"Crow, rooster, crow. Cocka-doodle-do. You have no time to play today for you must crow, rooster, crow. Cocka-doodle-do. You must tell all it's time to get up."[/code]You may have noticed something about the marks around the poem if you tried singular quotation marks: the apostrophe in "it's" ends the string, and will produce a syntax error because there's an open ' at the end.
  76. Make a new variable in the editor and call it poem. Then, copy that poem into that variable so you don't have to keep typing it.
  77. [code]poem = "Crow, rooster, crow.
  78. Cocka-doodle-doo.
  79. You have no time to play today for you must
  80. crow, rooster, crow.
  81. Cocka-doodle-doo.
  82. You must tell all it's time to get up."[/code]
  83. You can use p poem now if you want, but you already know it works.
  84. What we're doing here is seeing what we can do with string. Say you're not a fan of roosters. That's okay. We can compromise with Ruby. Have a line after you define the poem variable that says:
  85. [code]poem["rooster"] = "duck"[/code]To change the first iteration of the characters that spell rooster into duck. Now, it doesn't change the entire poem, but that's okay. I forgive me and in time you will too.
  86. So, what did we do? The square brackets, [ ], are like sights for finding a target on which to perform the operation. Our target was "rooster" and the operation was to change it to duck.
  87. You'll notice the \n things riddling the prompt. That's because there are line breaks in the poem. Don't worry about them, there are more important things to worry about.
  88. Like reversing the poem. What could possibly be more important? Try[code]p poem.reverse[/code]
  89. Well, that reversed the poem letter-by-letter. I didn't want that, exactly. I wanted to turn the lines around. Well, let's give it a shot.[code]p poem.lines.to_a.reverse[/code]:O Syntax error (for VX at least, I don't know if it happens on XP).
  90. What does that all do? Well, we selected the lines of the poem (.lines), converted them into an array or list (.to_a) and finally reversed them (.reverse).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement