Charas-Project

  • Home
  • Help
  • Search
  • Calendar
  • Login
  • Register
*
Please login or register.

Login with username, password and session length
 

News:

Click here to join us on IRC (#charas on irc.freenode.net)!



  • Charas-Project »
  • Off-Topic »
  • Archive »
  • Really Old Stuff »
  • General programming »
  • RUBY for Dummies
« previous next »
  • Print
Pages: [1]

Author Topic: RUBY for Dummies  (Read 6318 times)

Offline Trevlac


  • The Executioner
  • Agent
  • *
  • Posts: 942
RUBY for Dummies
« on: July 19, 2006, 02:51:20 AM »
Introduction

Ruby is a programming language at the core of RPG Maker XP. Ruby is a language in and of itself. RPG Maker uses what is called "RGSS" or Ruby Game Scripting System. Understanding this wonderous language called Ruby will help us better understand RGSS, and therefore, make better RPG's.

Firstly, I am going to explain some things. Ruby is an easy freaking language. Really easy, I'm talking, I learned all of the beginner stuff in one weekend. You will fly through Ruby with no trouble at all. It is a true OOL - or Object Oriented Language. This means that you create objects or certain structures within the program to make things easily accessable to whatever is running it.

If there ever was a professional-grade object oriented language, Ruby is it. Now, on to the zeroth lesson.

Lesson 0

Before we can just start coding in Ruby, we have to actually install it. Yes, install. Computers arn't capable of just going "Oh you entered {blah code}, that means I should do this!". Nope, you have to teach a computer how to do something before you actually tell it to do it.

Ruby 1.8.4 Final
24.41 MB
This is a link to the official creater's website's download. This is Ruby version 1.8.4, and it is stable. Just install this by following the guide.

Now you have it installed. Instead of opening notepad right away, take notice that it installed something called SciTe or Scintilla Text Editor. This thing is a WONDER! It color-codes your text depending on what language you select. But it's default settings are no good for us.  Download these two files and put them in your SciTe directory and REPLACE the old versions:

Global Properties - This will make the font monospace so you can see how the program looks in code better.
Ruby Properties - This changes the colors of certain things to help give your brain a better structure of the program.

The above step is OPTIONAL but it makes programming in Ruby so much easier.  Okay, now open SciTe and each time you open it, click "Languages" on the toolbar and select Ruby. This way you can use your custom settings. WARNING, if you notice your font is Arial, PM me, do NOT whine about it here.

Now go to View > Line Numbers, make sure that's checked. It'll make everything easier to see. You're ready for the first lesson!

Lesson 1

In SciTe, type this:

http://img.photobucket.com/albums/v410/trevlac/ruby/L1-0.gif

Save it as calc.rb. A .rb file is what a Ruby program is read as. Now go to the command prompt, find the directory you saved it in (use "cd " to get there) and type in "calc.rb".

It should show this:
Quote

5

Congratulations. You've just madea  program. How easy was that?  You made a program that added 2 and 3, and then displayed the result; 5. But now to explain something about numbers.

INTEGER versus FLOAT

In mathematics and programming, numbers without a decimal point is called an integer and numbers with decimal points are called floating-point numbers or floats for short.

Here are some integers:
Quote

0
999
5
10
-256
1234567890987654321

Here are some floats:
Quote

-5.12
666.66666666
123.4567
0.0
1.6180339887

Most of the time, programs do not use floats. They mostly use integers. I would be hardpressed to find a program that reads 5.8 emails, or a media player programmed to play 10.6 songs. Usually a float is used in a situation where percentage must be calculated (like 10% is the number times .1) or an RPG. But I can't recall a time in RPG Maker XP that I used floats.

Right now, we have a simple calculator. Acutally, calculators and computers always use floats, so if you are one of them, always use floats. Here are the methods of arithmetic!
Quote

+  Addition
- Subtraction
* Multiplication
/ Division (remember to use the forward slash and not the backslash "")

Let's do some simple arithmetic, shall we?

http://img.photobucket.com/albums/v410/trevlac/ruby/L1-1.gif

Here's what you should get when you run that program:
Quote

5.0
30.0
-1.0
4.5

Good. Everything in order, so let's try that with integers.

http://img.photobucket.com/albums/v410/trevlac/ruby/L1-2.gif

Here's what we get from that:

Quote

5
30
-1
4


Same answers right? Wrong. Take a look at that last answer. We got a 4 from 9/2! Why is this? Because that's integer division. The computer always round the answer of integer division down. The thing is, 4 is the right answer, it's just not what you expected.

What in the world is integer division good for? I'm going to use an analogy that the person who taught me Ruby - Chris Pine - used.

Let's say you're going to the movies and have $9. Let's pretend that movie prices are extremely low, and you can see one for $2. How many movies can you see there? 9/2 4. 4 Movies. 4.5 is not the right answer. They're not going to let you in to watch half of a movie! Or let half of you in to watch a whole movie!

This can be good for solving your Algebra homework too! Let's try some complex programs. You can see that Ruby follows the Order of Operations.

http://img.photobucket.com/albums/v410/trevlac/ruby/L1-3.gif

Results:
Quote

5
-29802


Please do the challenge programs now that the lesson is complete. The harder you work at these to finish them, the better you will get at Ruby! Make a program to calculate the following:

 
    How many hours are in a year?

 
    How many minutes are in a decade?

 
    How many minutes old are you?


Super challenge:
 
    If I am 945 million seconds old, how old am I?


When I write the next lesson, since we are done with numbers we will move on to letters.
Logged

Friends: MrMister, Dragonium, Shady Ultima, Tomi, Meiscool, BlackSkullWarlock, Ace of Spades, Smokey_Locs, Robotam, Blue_Strife, Apex

Offline Tomi

  • *does mannerism*
  • Leader
  • *
  • Posts: 2,000
(No subject)
« Reply #1 on: July 19, 2006, 03:00:24 AM »
Dude, you have such a way with words.  This tutorial is very informative and user friendly.  *huggles Trev*  I'm really looking forward to more.
Logged

Offline Trevlac


  • The Executioner
  • Agent
  • *
  • Posts: 942
(No subject)
« Reply #2 on: July 19, 2006, 10:11:36 PM »
YAY! Trevlac is Informative AND User-friendly. That practicly makes me a Windows/Linux hybrid! Oh my God...that's more profound that uniting Quantum Mechanics and General Relativity...
Logged

Friends: MrMister, Dragonium, Shady Ultima, Tomi, Meiscool, BlackSkullWarlock, Ace of Spades, Smokey_Locs, Robotam, Blue_Strife, Apex

Offline MrMister

  • damn u vile woman
  • Royal
  • *
  • Posts: 3,506
(No subject)
« Reply #3 on: July 19, 2006, 10:49:39 PM »
Anyone who didn't learn RUBY in 2004 is a lazy good-for-nothing bastardcrap. :|
Logged
you look like an orphan

Offline Trevlac


  • The Executioner
  • Agent
  • *
  • Posts: 942
(No subject)
« Reply #4 on: July 20, 2006, 10:24:11 PM »
Quote
Originally posted by MrMister
Anyone who didn't learn RUBY in 2004 is a lazy good-for-nothing bastardcrap. :|


That's exactly when I learned it...
Logged

Friends: MrMister, Dragonium, Shady Ultima, Tomi, Meiscool, BlackSkullWarlock, Ace of Spades, Smokey_Locs, Robotam, Blue_Strife, Apex

  • Print
Pages: [1]
« previous next »
  • Charas-Project »
  • Off-Topic »
  • Archive »
  • Really Old Stuff »
  • General programming »
  • RUBY for Dummies
 

  • SMF 2.0.10 | SMF © 2015, Simple Machines
  • XHTML
  • 2O11
  • RSS
  • WAP2
  • Simple Machines Forum