Author: http: //www.ruby-cn.org/ reproduced leave
1. Introduction Ruby is written by Japan's Yukihiro Matsumoto, easy to learn to object-oriented scripting languages, like Perl, rich in text processing, system management, etc., but Ruby is simple, easy to understand and expand. It is very similar to Python, but there is no Python in China. Generally speaking, he has the following characteristics:
Simple explanation type language (プ プ プ プ), so you don't have to compile (コ コ パ パ ル). Variables have no type of distinction, although it can save concerns about type errors, but will also cause the fragility of compilation. Variables do not need to be defined. Simple syntax, more from Eiffel inherit. No memory management is required, the system provides GC (ガ ガ ベ ベ ジ ジ ジ コレ タ タ) process mechanism. The pure object-oriented language is everything is an object, and the type of integer such as an integer is designed as an object from the beginning. Class (ラ ス), inheritance, Method (メソ ド ド), etc. (special way?) Special メソ メソ ド Module (モジュ モジュ ル) よ よ よ (テレ テレ) and closed bag (ク ロ ジャ ジャ). A resource defined at the beginning of the closure will be released after the closing package, suitable for socket, database connection, file handle, etc. Scripting Language Interpretation Executes Strong Text Processing Ability and Regular Expression Direct Access OS and writes system programs with Ruby. Other support for multiple long intenses, memory allows, can make large numbers of calculation operations. Have an abnormal processing capability, with an exception handling function of Java. Dynamic Loading (ダ ダ ナミ ナミ ク ディ ディ ディ グ). You can redefine yourself at runtime, and the class can also inherit / cancel inheritance at runtime. Thread. Now Ruby has a thread concept, Ruby2.0 seems to support local threads. Reflection. Ruby can see the inside of the program, such as whether a module contains a particular method, which Class, etc. of an object, etc. Similar to Java. Scalability. C API. Issue a license. Based on Ruby Artistic License (BSD style) and GPL. 2, environment installation
(1). Installation 1. You can download the source code from www.ruby-lang.org.
(2). Unzip the source file, enter the installation directory
#. / Configuremake
#Make
#Install
3. use
(1). Use IRB. IRB is an interactive interface that runs it in the shell, first display the prompt, wait for input; after the user input, it is processed, display the result to the user.
#irb irb (main): 001: 0> $ str = "Hello World! / n" => "Hello World! / N" IRB (Main): 002: 0> Print $ Str Hello World! => NIL IRB ( Main): 003: 0>
(2). Like other languages, run from program files
[root @ TSERVER / ROOT] # chmod a x foo.rb [root @ TSERVER / ROOT] # Cat foo.r #! / usr / local / bin / ruby -w puts "Hello, World!" [root @ TSERVER / root] # Ruby foo.rb hello, world! [root @ TSERVER / root] # ./foo.rb hello, world! [root @ TSERVER / ROOT] # (3). Ruby Usage Ruby [Option ...] [-] [programfile] [argument ...] pre>
For the command line parameters of Ruby, you can refer to the relevant documentation.
4. Simple example
Def SaygoodNight (Name) Result = "GoodNight," Name Return Resultens # Time for BED ... PUTS SAYGOODNIGHT ("John-Boy") PUTS SAYGOODNIGHT ("Mary-Ellen")
It can be seen that Ruby's grammar is still relatively simple, first, you don't have to write a semicolon every line, Ruby comment, until the end. Methods Definition begins with keyword DEF, then the method name and method parameters, Ruby does not need to define the program body with Braces, and only the keyword End is required. This procedure is also quite simple, the first line puts the `` goodnight, '' plus parameter Name, and assigns it to the local variable Result, and the second line returns the result to the caller. Note that we do not need to define the variable Result. Finally, we call 2 times, pass the results to the PUTS function, this function is simple to print to its parameters, the last result is like this:
GoodNight, John-BoyGoodNight, Mary-Ellen
In fact, PUTS SAYGOODNIGHT ("John-Boy") includes 2 functions calls, a PUT system function, a SayGoodNight function. But why does PUTS call does not use parentheses? In fact, the following calls are equivalent:
Puts (SaygoodNight "John-Boy) PUTS (SaygoodNight (" John-Boy ") Puts SaygoodNight (" John-Boy ") Puts SaygoodNight" John-Boy "
But if you don't write parentheses, do you know who is the parameter passed? So, it is recommended to add parentheses after the method for easy access to the source program.
This method also shows the String object, there are many ways to create a String object, but the most common calculations use string literals: single quotes or two sets of characters. Their difference is that Ruby builds the operations to do when these two strings are built. For a string caused by single quotes, Ruby will work very little, and the single quotes are partially part of it. If it is a double quotation, you have to do more work. First, it checks if it contains a backslash, which is an escar, and then replaced with the appropriate binary value. The most common is "/ n", it will be replaced. Such as:
Puts "and goodNight, / NGRANDMA" results are as follows: And GoodNight, Grandma
The second thing is that expression interpolation. # {expression} is replaced by the value of Expression, for example, the following method and the same result is the same result.
Def SayGoodNight (Name) Result = "GoodNight, # {name}" Return Resultens
Of course we can also simplify this function.
The result returned by a Ruby function is the value of the last row, so this function can be written as follows:
DEF SAYGOODNIGHT (NAME) "GoodNight, # {name}" end
Ruby is named using a definitive naming mode for variables, the first letter of the variable name marks its type, is a local variable or a method parameter, the method name should begin with lowercase letters or underscore, the global variable should begin, instance The variable starts with @, the class variable is in @@, and the class name, the module name, and constant should begin with uppercase letters.
A name can be any combination of letters, numbers, and underscore, but there is @ @ @ 不 不 允 允 跟 一 跟 一.
In short, as a language, not one or two examples, several articles can be clear, I will always see a reason for the 10 reasons of I love Ruby, I will always see a reason, I will always see a reason. It is Fun with Ruby. Perhaps, more interesting is still behind.