RUBY Beginner's Tutorial learn Ruby_ruby with me

Source: Internet
Author: User
Tags comments inheritance prepare uppercase letter

Learn Ruby with me.
by Tiger
Note: This tutorial reproduces free game Pioneer Forum, copyright belongs to the author Tiger.
First article
Second article

First Article
Preface
I'm going to start learning Ruby from today. What do you say, I haven't seen you? A new person to write a tutorial. With my strong perseverance, scientific self-study methods, as well as my research spirit, I am confident to learn Ruby, but also the novice you can! Because this tutorial is rookie teach Lai bird if there is anything wrong place also please seniors advice! If no one is watching, I will be happy as a fool.
======================================================================
Volume of the first language
--------------------------------------------------------------------------
Prepare knowledge
One, why should you learn Ruby?
Needless to say, it must help to realize the potential of RM. And how bad Ruby is, I don't have anything to do with it, because I learn it for one reason--rm!
Two, what is Ruby?
It is a kind of pure object-oriented programming scripting language. It's like the C language, is a computer to understand the language, like you to the computer yell "I want to surf the internet" it will not let you surf the Internet, must be a language to tell it, Ruby is one of them, of course, Ruby also need to play, not to say ...
What do you want to learn Ruby? RM, of course, and I've prepared three things:
Notebook-Don't look down on it, this is the most important, some of your questions and GUI sense, the conclusions of the study can be recorded on it.
A new project, which I temporarily named Test, will be used for research and practice.
Another new project, which I've named the standard for the time being, is that if you change your own ruby to unrecognizable, it can be used as a reference. Of course RM's script data is stored in the Scripts.rxdata file in the Data folder, and you can also back up the file.
--------------------------------------------------------------------------
Practical Basis
one, about script Script Editor:
. RM, the way to enter the script Script Editor is to press F11 or choose Script Editor from the main Menu Tools menu ...
. Some shortcut keys in the Script editor (thank Carol3, this is not what I found):
TAB: Can be jumped, can be automatically indented
CTRL + Mouse wheel: resizing text
CTRL + F: Find this page script
CTRL + SHIFT + F: Find all scripts
CTRL + H: replace
CTRL + A: Select All
CTRL + C,ctrl + V: Copy paste
CTRL + Z,ctrl + Y: Undo and Redo
F3: With Ctrl + F, "Find Next"
F4,F5: Select previous/Next script
F6: The name of the chosen line
CTRL + ENTER: Save and close this window
Ii. steps to create a program:
. Determine the purpose of the program--for example, your goal is to do a procedure that asks for a triangular area.
. Determine what method to use-I know s=1/2ah=1/2absinc, the angle is too annoying, and the latter is not a separate multiplication and division operations, so I choose the former.
. Create a program.
third, the programming should develop the good habit:
. Write a comment. Many people think that writing a note is a waste of time--but don't forget--you may have a clear idea when you're writing code, but when the program gets bigger, or when you're faced with code written a few months ago, the annotation's role is obvious.
In Ruby, a whole line of text following the # symbol is identified as a comment. When you want to write large chunks of comments, you can also use the =begin and =end in the middle of the comments.
Annotations are green, do not execute, and do not affect the operation of the program and are used only as a writer-marked memo record.
Not completed ...
Iv. the idea of compiling errors
When you have errors in spelling and grammar, the program will stop and tell you the relevant information, and then press F11 into the Script editor and you will see the cursor in the wrong line.
As shown in figure:
Rows: Uninitialized Constants "Persond"
I haven't summed up the specific method yet. Not completed ...
--------------------------------------------------------------------------
Type&run
(It is not intended to be sent, but some people say this tutorial writes some out of RM, so sent the following--not ready to send the reason is I do not know Carol3 "RGSS area top script collation, the current collection script: 30, hope you add" a post in unexpectedly have a tutorial! And a part of my almost the same, forget, in order to ensure the integrity of the tutorial, I wrote my hair up ... Not plagiarism! )
Let's get started. Ruby:
Practice: Part of the title screen customization
Practical purposes:
. Learn Ruby's research methods.
. Learn to do some experiments according to the notes.
. The use of some shortcut keys in the Scripting Editor.
Open Script Script Editor
. Find Scene_title on the left side of the script Script Editor
. You can see the first line in the basket on the right:
#=====================================================================
# Scene_title
#------------------------------------------------------------------------------
# The class that handles the title screen
#=====================================================================
We know this is a comment, it means scene_title This code is used to handle the title screen. In the same way, we can take a look at other groups (the group is similar to the scene_title in the Script Editor to the left of a channeling of code), how, their use at a glance!
Now that we know it's for the title screen, let's Take the axe!
Visit: Read the database, generate system objects; These are too esoteric to look down--
S1 = "New Game"
S2 = "Continue"
S3 = "Exit"
Finally saw the familiar Chinese word, ah! This is not an option for the title screen in RM!
Look at the comment above it--Generate a command window. Sure enough, the title screen option.
We changed it, try it:
S1 = "The new reincarnation"
S2 = "Old Memories"
S3 = "Gone With the Wind"
Press CTRL + ENTER: Save and close this window.
Then press F12 to run the game. It worked!
. People's ambition is always unlimited, I also want to add a about this game! Okay, try again:
S1 = "The new reincarnation"
S2 = "Old Memories"
S3 = "Gone With the Wind"
S4 = "About this game"
. Greed always has some guilty, and then look at the contents of the following to run the game!
@command_window = window_command.new (S1, S2, S3)
@command_window. back_opacity = 160
@command_window. x = 320-@command_window. WIDTH/2
@command_window. y = 288
There are x, Y big cover is to say the box of options! Bad, one more option, the box must be longer, if not to fix the box of the property options will appear when the option is not displayed. It's a pipe! Even if it's true, there's nothing I can do.
The first line has a (the S1, [the S2, the S3]) S1,s1,s3 seems to be the same thing as S1 in S1,S1,S3 = "New Reincarnation"? Now that I've added S4, I have to add the S4 as well:
@command_window = window_command.new (S1, S2, S3,S4)
S4;s3 to learn it between the sample also used, connected.
. Run the game again. Sure enough, the game options are available and the box can be adjusted automatically to include four options. We can see that the guess is wrong, there are X, Y big cover is to say the box
's position?
The picture shows that the relative position of the stars in the red box and the top of the frame has not changed before and after the modification, and the lower end of the box and RPG characters relative position of the relative position has changed, that is, the top of the frame is the same position, when there are new options, automatic extension of the lower end. And the X, y cover is the upper position of the box that says the option?
. Try again:

Copy Code code as follows:

S1 = "The new reincarnation"
S2 = "Old Memories"
S3 = "Gone With the Wind"
S4 = "About this game"
S5 = "ASD"
S6 = "SDASD"
S7 = "Weqw"
@command_window = window_command.new (S1, S2, S3,S4,S5,S6,S7)

Sure enough, there's a headline that doesn't show.
. Look again:
S1 = "The new reincarnation"
S2 = "Old Memories"
S3 = "Gone With the Wind"
S4 = "About this game"
@command_window = window_command.new (S1, S2, S3,S4,S5)
This can be an error.
S1 = "The new reincarnation"
S2 = "Old Memories"
S3 = "Gone With the Wind"
S4 = "About this game"
@command_window = window_command.new (S1, S2, S3)
About this game will not be shown.
. There is no use for this game now. We can refer to the "new reincarnation" "Old Memories" "with the Wind" to give it function. Whatever they have I have to add to this game. Look down and notice that there are:
def update
@command_window. Update
# when you press the C key
If Input.trigger? (INPUT::C)
Case @command_window. Index
When 0 # New game
Command_new_game
When 1 # go on
Command_continue
When 2 # exit
Command_shutdown
End
End
End
when-when ... , this is like a conditional part of a conditional disagreement that tells me this is the choice to choose a new game;
Then Command_new_game;command_continue;command_shutdown is the action. Will they be what Ruby has already defined? If so, it is useless for you to write something else about this game. Then let's share one thing about this game and quitting:
Copy Code code as follows:

def update
@command_window. Update
# when you press the C key
If Input.trigger? (INPUT::C)
Case @command_window. Index
When 0 # New game
Command_new_game
When 1 # go on
Command_continue
When 2 # exit
Command_shutdown
When 3
Command_shutdown
End
End
End

Run it, sure enough! Click on the game on the exit. Visible Command_shutdown is used to invoke the Exit command. Would it be something that was already defined in Ruby? Let's look further down:
.
#--------------------------------------------------------------------------
# command: New game
#--------------------------------------------------------------------------
.
.
.
.
.
.
.
#--------------------------------------------------------------------------
# command: Continue
#--------------------------------------------------------------------------
.
.
.
.
.
.
.
#--------------------------------------------------------------------------
# command: Exit
#--------------------------------------------------------------------------
This seems to be a sort of discussion. They all start with def xxxxxxx, and the content of xxxxxxx is the content of the options in the previous disagreement. So it's not something that is already defined in Ruby. That leaves room for our own definition:
Copy Code code as follows:

def update
@command_window. Update
# when you press the C key
If Input.trigger? (INPUT::C)
Case @command_window. Index
When 0 # New game
Command_new_game
When 1 # go on
Command_continue
When 2 # exit
Command_shutdown
When 3
Abc
End
End
End

Copy the command: Quit the content and paste the Def Command_shutdown in the pasted content into def ABC after exiting. Run the same effect as before.
Well, as far as I'm right now, I can only do this, and if you know some grammar, change the contents of the Def ASD below.
--------------------------------------------------------------------------
Summarize
Not completed ...
--------------------------------------------------------------------------
Homework
. Steps for program development?
Try using some shortcut keys in the Script Editor.
Raise the question:
Try to summarize some of the error types and their views that appear in the Script Editor's use.
Not completed ...
--------------------------------------------------------------------------------------------------------------- ----------
Answer
Slightly

Second article
Chapter One-Class, object and method
--------------------------------------------------------------------------
Prepare knowledge
Now that Ruby is a purely object-oriented scripting language, I start with the object.
What is the object?
Everything in the game can be interpreted as an object.
The number of money is the object, the village head is the object, Rogue armour is also like. Because Ruby can handle values that are objects, the concept of classes is introduced to facilitate the manipulation of objects.
Second, what is the class?
The type of object is called a class.
Village head and Rogue a belong to NPC class, the number of money belongs to the natural number class (note: Here the class you can set yourself, you can also say village head and rogue a belongs to human: belong to mammals: belong to ... )
Some special objects belonging to this class are called entities (or instances) of the class.
In daily life, we have a certain level of classification of all objects.
We know: Biology animals mammals human
The village head and the rogue a belong to the human, it must have the attribute of the mammal class, also must have the attribute of the animal. We call biology the parent of animals, animals are biological subclasses, it is not difficult to find that subclasses inherit the properties of the parent class. Of course, some of the properties of the parent class may not be inherited by a particular subclass-although birds can fly, penguins are a subclass of birds that cannot fly.
Inspired by the laws of nature, Ruby introduced inheritance. It greatly reduces the programmer's workload, we only need to define a parent class (or superclass) of the attribute, its subclass will automatically inherit its parent class properties. Of course, when you encounter the Penguin case as mentioned above, the difference between the subclass and the parent class is also added or redefined. The meaning of inheritance is to keep the nature of the successor but at the same time have its own unique characteristics.
The introduction of the class is only convenient for the operation of the object, then how exactly the object of operation? That's the way it's going to work.
Third, what is the method?
Method is a command that is private to all objects in a class.
You simply invoke a method on an object to allow the object to execute the method represented by this method. The object being manipulated is called "action instance" or "parameter of method".
For example, you have a lot of dogs, (a dog is a class, your dog is its subclass) has a name Richter (Richter is an object, also, you train them to sit down every day (this is to define a method), and then every time you send a number to sit down this command (this is the way to sit down) the dog you raise will sit down. (You do the work on the object by means of a method). If your dog is divided into dogs and pug, (dogs and Pug are subclasses of your dog), they inherit the methods defined in the parent class. Of course there are many ways to define a class (You can teach a dog a lot of things).
Think of Ruby as a CD player, all the things that the CD player operates--CD is all objects, class is the style of the song (such as R&m is a class, Rap is another); Of course, you can also take the singer as a kind of division (such as Jay Chou is a class, Eason Chan is another class), The calling method is to press the play button, stop the button, pause the button, and so on.
Practical basis
The steps to create an object in Ruby and manipulate it are as follows:
First you have to define the properties of the class--
Definition of a class
Format:
Class name
Content defined in a class
End
Note: The class name must begin with an uppercase letter.
If it's a subclass, write this:
Class Subclass name < parent class Name
Content defined in a class
End
or written directly after the definition:
Class Subclass name < parent class Name
End
This just makes the parent class inherit from the parent class, not redefine it. Why do you say that?
The light has a class not yet, then plug the method into the inside so that the object can be manipulated later.
Ii. definition of the method
The definition of the method is written in the content defined in the class.
Format:
Def
Content defined in a method
End
Note: The method must begin with a lowercase letter.
Of course, when you encounter a particular subclass of the Penguin as mentioned above, you also need to redefine the distinction between subclasses and parent classes, which is called overload, and the format is the same. The invocation of a method, if repeatedly defined, is the last time.
Use the keyword super when calling the same method in the parent class in a subclass method. Super will invoke the same method in the parent class that is overridden by the current method.
Format:
Super (definition of content, ...)
Omitting parentheses and arguments will pass the arguments of the current method to the same method in the parent class as if it were the same. If you do not want to use parameters when calling, use parentheses to explicitly mark out, like super ().
Finally, create an object (instance) that belongs to this class--
Iii. creation of examples
Format:
Instance name = Class name. New
The instance has finally been created, and it is time to operate on it.
Four, the method calls
In Ruby, we get through the dots. To invoke the method of an object
Format:
The name of the instance that was created. Method name
If you want to create an instance of a temporary existence and add a method to it:
(class name. New). Method name
Or:
Class name. New. Method Name
Copy Code code as follows:

Class NPC
def intro
Print "I'm NPC?"
End
End
Class LIOUMANG<NPC
Def talk
Print "XX in the south of the village"
End
End
Rogue Armour =lioumang.new
Rogue A. Talk #显示 am I NPC?
Rogue A. Intro #因为流氓甲继承了它的父类的方法, so show xx in the south of the village

Description
~5 defines a class--NPC
~11 defines a subclass of an NPC--lioumang
~4 defines a method called Intro for the NPC, and print "" is used to display the contents of "". (Don't care how it works first)
~10 defines a method called Talk for Lioumang.
Creating a case--rogue armour
Method of calling talk for Rogue armour
Method of calling intro for Rogue armour

Two
Copy Code code as follows:

Class NPC
def intro
Print "AAA"
End
End
Class LIOUMANG<NPC
def intro
Print "BBB"
End
End
Rogue Armour =lioumang.new
Rogue armour. Intro

Description
Show is BBB, visible method of the Shou Hammer cooking ㄒ dan  蛞 Slaughter swear Don Huai  muscle 5 curled? 3 lines into Rogue a =npc.new show AAA
Three
Copy Code code as follows:

Class NPC
def intro
Print "AAA"
End
Def talk
Print "CCC"
End
End
Class NPC
def intro
Print "BBB"
End
End
Rogue Armour =npc.new
Rogue armour. Intro
Rogue armour. Talk

Description
First show is BBB, then show CCC, the visible class such as repeatedly defined, then to retain their intersection of the complement, their intersection with the last time prevail. But if 13 lines become rogue Armour =npc.new will show AAA.
Four
Copy Code code as follows:

Class NPC
Def talk (A=nil) #nil为空集
P A
End
End
Class Lioumang < NPC
Def talk (a)
Super (5) # is called with 5 as a parameter
Super (a) # is called with 5 as a parameter
Super # is called with 5 as a parameter, and the shorthand for super (a)
A = 1
Super # is called with 1 as a parameter, and the shorthand for super (a)
Super () # Call with no parameters
End
End
Lioumang.new.talk 5

Description
The function of the ~4 method is to display variable A, and A=nil, which displays both nil
Visible: When super brackets and arguments are omitted, the parameters of the current method are passed intact to the same method in the parent class. If you do not want to use parameters when calling, use parentheses to explicitly mark out, like super ().

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.